0

I have made a large app in Python using the PyQt5 module. Recently I figured I wanted to separate some of the functionality into different windows available from a main window. But I think I have some scope-issues with how I went about doing so. It's apparent that there's something I'm not quite grasping. I've tried searching around for similar issues, and while there are a lot of questions on the same topic, none to my knowledge use the same kind of setup (I "learned" from an autogenerated example with PyQt5 Designer Tool).

I've reproduced the issue in the code-snippet below. The issue is that any functions called in the new window, called from the first window, does not work.

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowTitle(_translate("MainWindow", "pyqt5test"))
        MainWindow.resize(200, 200)
        # Connecting a central widget to the main-window
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        # Adding a text-box to the central widget.
        self.text_box = QtWidgets.QTextEdit(self.centralwidget)
        self.text_box.setGeometry(QtCore.QRect(20, 95, 120, 25))
        self.text_box.setObjectName("text_box")
        # Adding a print-button
        self.print_btn = QtWidgets.QPushButton(self.centralwidget)
        self.print_btn.setGeometry(40, 120, 75, 20)
        self.print_btn.setObjectName("print_btn")
        self.print_btn.setText(_translate("MainWindow", "Print"))
        # Adding new window-button
        self.new_window_btn = QtWidgets.QPushButton(self.centralwidget)
        self.new_window_btn.setGeometry(0, 20, 100, 20)
        self.new_window_btn.setObjectName("new_window_btn")
        self.new_window_btn.setText(_translate("MainWindow", "New Window"))
        # Functionality
        self.print_btn.clicked.connect(self.print_text)
        self.new_window_btn.clicked.connect(self.new_window)


    def print_text(self):
        print(self.text_box.toPlainText())


    def new_window(self):
        self.newWindow = QtWidgets.QMainWindow()
        ui = Ui_different_window()
        ui.setupUi(self.newWindow)
        self.newWindow.show()

class Ui_different_window(object):
    def setupUi(self, differentWindow):
        _translate = QtCore.QCoreApplication.translate
        differentWindow.setObjectName("differentWindow")
        differentWindow.setWindowTitle(_translate("differentWindow", "Second Window"))
        differentWindow.resize(200, 200)
        # Connecting a central widget to the main-window
        self.centralwidget = QtWidgets.QWidget(differentWindow)
        self.centralwidget.setObjectName("centralwidget")
        differentWindow.setCentralWidget(self.centralwidget)
        # Adding a text-box to the central widget.
        self.text_box = QtWidgets.QTextEdit(self.centralwidget)
        self.text_box.setGeometry(QtCore.QRect(20, 95, 120, 25))
        self.text_box.setObjectName("text_box")
        # Adding a print-button
        self.print_btn = QtWidgets.QPushButton(self.centralwidget)
        self.print_btn.setGeometry(40, 120, 75, 20)
        self.print_btn.setObjectName("print_btn")
        self.print_btn.setText(_translate("differentWindow", "Print"))
        # Functionality
        self.print_btn.clicked.connect(self.print_text)


    def print_text(self):
        print(self.text_box.toPlainText())


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
RoyM
  • 735
  • 3
  • 14
  • 1
    change `ui = Ui_different_window() ui.setupUi(self.newWindow)` to `self.ui = Ui_different_window() self.ui.setupUi(self.newWindow)` – eyllanesc Jun 11 '19 at 23:16
  • @eyllanesc Oh wow, it was that simple. I don't know how I missed it. I put the self-references right above and below to make it appear at all, and I still didn't realize. – RoyM Jun 11 '19 at 23:22
  • 1) ui is a local variable so it will be removed a moment after displaying the widget so the print_text method can not be accessed, instead the buttons, Qtextedit, etc. are maintained because the ownership has it c++. 2) You must read the PyQt5 docs about the use of class generated by Qt Designer: https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html#using-the-generated-code, an example based on it and your code is: https://gist.github.com/eyllanesc/16c1052e1ae17b91140be8bf72981a7e – eyllanesc Jun 11 '19 at 23:24

0 Answers0