Passing data to another window in PyQt

Posted by


Passing data between windows in PyQt can be achieved in several ways. One common method is by using signals and slots to communicate between the windows. In this tutorial, we will go through the steps to pass data from one window to another in PyQt using signals and slots.

Step 1: Import the necessary modules
First, you need to import the necessary modules for PyQt. You will need to import the QtCore, QtGui, and QtWidgets modules.

from PyQt5 import QtWidgets, QtCore

Step 2: Create the main window
Next, you need to create the main window where the data will be inputted. In this example, we will create a simple main window with a button that opens a second window when clicked.

class MainWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Main Window")

        self.button = QtWidgets.QPushButton("Open Second Window")
        self.button.clicked.connect(self.open_second_window)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.button)

        self.setLayout(self.layout)

    def open_second_window(self):
        self.second_window = SecondWindow()
        self.second_window.show()

Step 3: Create the second window
Next, you need to create the second window where the data will be displayed. In this example, we will create a simple second window that displays the data passed from the main window.

class SecondWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Second Window")

        self.label = QtWidgets.QLabel()

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.label)

        self.setLayout(self.layout)

    def update_label(self, data):
        self.label.setText(data)

Step 4: Modify the main window to pass data to the second window
In the main window class, we need to modify the open_second_window method to pass the data to the second window.

def open_second_window(self):
    self.second_window = SecondWindow()
    self.second_window.show()

    self.second_window.update_label("Hello, World!")

In the open_second_window method, we create an instance of the second window and show it. We then call the update_label method of the second window instance and pass the data we want to display.

Step 5: Run the application
Finally, we need to create an instance of the main window and run the application.

if __name__ == "__main__":
    app = QtWidgets.QApplication([])
    main_window = MainWindow()
    main_window.show()
    app.exec_()

By following these steps, you can pass data between windows in PyQt using signals and slots. This method allows for easy communication between different windows in your PyQt application. Feel free to customize the example above to fit your specific needs and requirements.

0 0 votes
Article Rating

Leave a Reply

17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@gigsoll
3 hours ago

Thank you dude. Amazing tutorial what save me hours of hitting of a wall with my head

@MathTutorVideos
3 hours ago

How about going the other-direction? If you want to send from the child window 'second window' to the parent window First Window?

Ex. Second Window has a QLineEdit and the person types "Apple".
How to make FirstWindow's label say "Apple"? How to send QLineEdit.text() from secondWindow's value to the first-window?

@aminelouguit2008
3 hours ago

thank you for this video 🙂 plz i have a question i added a 3 window and when i try to go from the second window to the third window i got an error said that : NameError: name 'Ui_SecondWindow' is not defined !! can you help please 🙂 ?

@silpisingha8140
3 hours ago

Very good tutorial really help me thanks ❤

@pablorc8091
3 hours ago

Thank you for sharing your knowledge. This helped me!

@pragyanchoudhury5416
3 hours ago

Thank you soo much Ajay <3 I've been stuck on using a technique for switching back and forth between windows and everything else kept failing.. You saved me… Only your tutorial worked without any issues… Love you man …

@jainishsakhidas4571
3 hours ago

What to do if we want to pass the data from the second window to the first main window

@reedler21
3 hours ago

Thanks Ajay you are a legend. The other answers across stackoverflow seem to suggest using global variables with an .exec() call which is outrageous! This is the best way, thanks man

@michaelbutler6561
3 hours ago

Another great how-to. Thanks again Ajay

@frya446
3 hours ago

Thank you and greetings!

@techdevrpa
3 hours ago

thank so much guy! could you record class of django as well ?

@dheerajtrivedi4917
3 hours ago

Hi Ajay,

How to save the input data and display it next time in the existing window.

@tamralipti
3 hours ago

How to close the second window and reopen the first window that I have hidden while opening the second window?

@basilcmr
3 hours ago

Need a message from second window back 1st window and just close the second window instead of the whole application.

@EzioOmer
3 hours ago

thank you friend

@luanrodrigues598
3 hours ago

Thank you ❤❤❤

@canalhelengui2.051
3 hours ago

=D

17
0
Would love your thoughts, please comment.x
()
x