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_())