How to send information between windows in PyQt5?

22 views Asked by At

So. I've asked this question once as a generic question, but this time I'll do it with code appended from the get go.

I'm trying to pass information between two windows. The first window is a window with two labels, top and bottom, and the second window is for editing those labels.

Both windows are created with qt designer and are in seperate files. I've tried different methods by I just can't seem to get the two windows to connect properly.

Once I press the button in the first window, the second window briefly opens and then shuts down again, but if I "grey out" the line: self.ui.update_label.connect(self.update_labels) with a # (in the edit_labels function in first window), then the new window opens (allthough then it's not connected to the first window).

Can somebody please let me know what I'm doing wrong?

FirstWindow.py

from PyQt5 import QtCore, QtGui, QtWidgets

from SecondWindow import Ui_SecondWindow


class Ui_FirstWindow(object):
    def setupUi(self, FirstWindow):
        FirstWindow.setObjectName("FirstWindow")
        FirstWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(FirstWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.top_label = QtWidgets.QLabel(self.centralwidget)
        self.top_label.setGeometry(QtCore.QRect(80, 110, 331, 111))
        self.top_label.setObjectName("top_label")
        self.bottom_label = QtWidgets.QLabel(self.centralwidget)
        self.bottom_label.setGeometry(QtCore.QRect(80, 290, 47, 13))
        self.bottom_label.setObjectName("bottom_label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked= lambda: self.edit_labels())
        self.pushButton.setGeometry(QtCore.QRect(580, 240, 171, 71))
        self.pushButton.setObjectName("pushButton")
        FirstWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(FirstWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        FirstWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(FirstWindow)
        self.statusbar.setObjectName("statusbar")
        FirstWindow.setStatusBar(self.statusbar)

        self.retranslateUi(FirstWindow)
        QtCore.QMetaObject.connectSlotsByName(FirstWindow)

    def retranslateUi(self, FirstWindow):
        _translate = QtCore.QCoreApplication.translate
        FirstWindow.setWindowTitle(_translate("FirstWindow", "MainWindow"))
        self.top_label.setText(_translate("FirstWindow", "TextLabel"))
        self.bottom_label.setText(_translate("FirstWindow", "TextLabel"))
        self.pushButton.setText(_translate("FirstWindow", "Edit labels"))

    @QtCore.pyqtSlot(str, str)
    def update_labels(self, top_line_text, bottom_line_text):
        self.top_label.setText(top_line_text)
        self.bottom_label.setText(bottom_line_text)

    def edit_labels(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_SecondWindow()
        self.ui.setupUi(self.window)
        self.ui.update_label.connect(self.update_labels)
        self.window.show()



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    FirstWindow = QtWidgets.QMainWindow()
    ui = Ui_FirstWindow()
    ui.setupUi(FirstWindow)
    FirstWindow.show()
    sys.exit(app.exec_())

SecondWindow.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_SecondWindow(object):

update_label = QtCore.pyqtSignal(str, str)

def setupUi(self, SecondWindow):
    SecondWindow.setObjectName("SecondWindow")
    SecondWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(SecondWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.top_label_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
    self.top_label_lineEdit.setGeometry(QtCore.QRect(110, 120, 421, 20))
    self.top_label_lineEdit.setObjectName("top_label_lineEdit")
    self.bottomLabel_lineEdit = QtWidgets.QLineEdit(self.centralwidget)
    self.bottomLabel_lineEdit.setGeometry(QtCore.QRect(110, 160, 421, 20))
    self.bottomLabel_lineEdit.setObjectName("bottomLabel_lineEdit")

    self.submit_pushButton = QtWidgets.QPushButton(self.centralwidget, clicked= lambda: self.on_submit())
    self.submit_pushButton.setGeometry(QtCore.QRect(280, 200, 75, 23))
    self.submit_pushButton.setObjectName("submit_pushButton")

    SecondWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(SecondWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
    self.menubar.setObjectName("menubar")
    SecondWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(SecondWindow)
    self.statusbar.setObjectName("statusbar")
    SecondWindow.setStatusBar(self.statusbar)

    self.retranslateUi(SecondWindow)
    QtCore.QMetaObject.connectSlotsByName(SecondWindow)

def on_submit(self):
    self.update_label.emit(self.top_label_lineEdit.text(), self.bottomLabel_lineEdit.text())
    self.close()

def retranslateUi(self, SecondWindow):
    _translate = QtCore.QCoreApplication.translate
    SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
    self.submit_pushButton.setText(_translate("SecondWindow", "Submit"))

I've tried following along to Alan D Moores coding tutorial on the subject on Youtube and it seems logical that you would create a connection in this way, but I just can't seem to get it to work.

0

There are 0 answers