PyQT5: How to open and close 3 windows consecutively

77 views Asked by At

I am have a problem in the process of my GUI. I have 3 window which is the main window -> 2nd window -> 3rd window then exit. It works well when clicking the button going to 2nd window, but if I clicked the button to window 3 it crushes.

The whole process crushes without saying any error.

This is my Code for the first window:

from window2 import *
from window3 import *

class Ui_MainWindow(object):
def openWindow2(self):
    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_MainWindow2()
    self.ui.setupUi2(self.window)
    MainWindow.hide()
    self.window.show()

def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(637, 309)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(230, 90, 341, 16))
    self.label.setObjectName("label")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(520, 230, 75, 23))
    self.pushButton.setObjectName("pushButton")

    self.pushButton.clicked.connect(self.openWindow2)

    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

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

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

This is the code for the 2nd window:

from window3 import *
class Ui_MainWindow2(object):

def openWindow3(self):

    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_MainWindow3()
    self.ui.setupUi3(self.window)
    MainWindow.hide()
    self.window.show()

def setupUi2(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(481, 242)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(340, 180, 75, 23))
    self.pushButton.setObjectName("pushButton")

    self.pushButton.clicked.connect(self.openWindow3)

    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(210, 100, 61, 16))
    self.label.setObjectName("label")
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

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

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow2()
    ui.setupUi2(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

This is the code for the 3rd window:

from window1 import *
from window2 import *

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow3(object):
def setupUi3(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(397, 319)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(180, 110, 47, 13))
    self.label.setObjectName("label")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(290, 250, 75, 23))
    self.pushButton.setObjectName("pushButton")
    MainWindow.setCentralWidget(self.centralwidget)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

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

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow3()
    ui.setupUi3(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Can someone tell me why the process crushes in the 2nd window when button clicked and why it cannot proceed to the 3rd window?

1

There are 1 answers

6
S. Nick On BEST ANSWER

Try it:

window1.py

from window2 import *
from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def openWindow2(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow2(self.window)                # +++ self.window
        self.ui.setupUi2(self.window)
        MainWindow.hide()
        self.window.show()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(637, 309)
        MainWindow.setWindowTitle('Window 1')
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(230, 90, 341, 16))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton("Button 1", self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(520, 230, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.openWindow2)

        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

#        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)



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

window2.py

from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow2(object):
    def __init__(self, window2, *args, **kwargs):              # +++
        super(Ui_MainWindow2, self).__init__(*args, **kwargs)  # +++
        self.window2 = window2                                 # +++  

    def openWindow3(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow3()
        self.ui.setupUi3(self.window)

#        MainWindow.hide()                                     # ---
        self.window2.hide()                                    # +++

        self.window.show()

    def setupUi2(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(481, 242)
        MainWindow.setWindowTitle('Window 2')
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton("Button 2", self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(340, 180, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.openWindow3)

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(210, 100, 61, 16))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

#        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow2()
    ui.setupUi2(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

window3.py

from window1 import *
from window2 import *

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow3(object):
    def setupUi3(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(397, 319)
        MainWindow.setWindowTitle('Window 3')
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(180, 110, 47, 13))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton("Button 3", self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(290, 250, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

#        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow3()
    ui.setupUi3(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

enter image description here


Update

window1.py

from window2 import *
from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def openWindow2(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow2(self.window)                # +++ self.window
        self.ui.setupUi2(self.window)
        MainWindow.hide()
        self.window.show()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(637, 309)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(230, 90, 341, 16))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(520, 230, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.openWindow2)

        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Window 1"))
        self.pushButton.setText(_translate("MainWindow", "Button 1"))
        #...        

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

window2.py

from window3 import *
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow2(object):
    def __init__(self, window2, *args, **kwargs):              # +++
        super(Ui_MainWindow2, self).__init__(*args, **kwargs)  # +++
        self.window2 = window2                                 # +++  

    def openWindow3(self):
        self.window = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow3()
        self.ui.setupUi3(self.window)

#        MainWindow.hide()                                     # ---
        self.window2.hide()                                    # +++

        self.window.show()

    def setupUi2(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(481, 242)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(340, 180, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.pushButton.clicked.connect(self.openWindow3)

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(210, 100, 61, 16))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Window 2"))
        self.pushButton.setText(_translate("MainWindow", "Button 2"))
        #...        


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow2()
    ui.setupUi2(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

window3.py

from window1 import *
from window2 import *

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow3(object):
    def setupUi3(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(397, 319)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(180, 110, 47, 13))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 250, 95, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.pushButton.clicked.connect(MainWindow.close)              # +++

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Window 3"))
        self.pushButton.setText(_translate("MainWindow", "Button 3 (Quit)"))
        #... 

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow3()
    ui.setupUi3(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())