PyQt5: как открыть и закрыть 3 окна последовательно

#user-interface #pyqt5 #qwindow

#пользовательский интерфейс #pyqt5 #qwindow

Вопрос:

У меня проблема в процессе работы моего графического интерфейса. У меня есть 3 окна, которые являются главным окном -> 2-е окно -> 3-е окно, затем выйдите. Это хорошо работает при нажатии кнопки, переходящей во 2-е окно, но если я нажал кнопку в окне 3, она отключается.

Весь процесс завершается без сообщения о какой-либо ошибке.

Это мой код для первого окна:

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

Это код для второго окна:

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

Это код для третьего окна:

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

Может кто-нибудь сказать мне, почему процесс завершается во 2-м окне при нажатии кнопки и почему он не может перейти к 3-му окну?

Ответ №1:

Попробуйте:

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

введите описание изображения здесь


Обновить

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

Комментарии:

1. Спасибо сэру С.Нику, это работает, но как я могу закрыть окно 3, когда нажимаю кнопку 3? потому что я пытался сделать def close(self): Mainwindow.close() , но это не может закрыть окно 3? Как я могу это закрыть?

2. Добавьте строку self.pushButton.clicked.connect (MainWindow.close) в window3.py

3. Спасибо, сэр, я действительно ценю помощь.

4. нужно ли мне комментировать все self.retranslateUi(MainWindow) ? потому что это удаляет все компоненты, которые я использовал

5. я все эти часы пытался внедрить это в свои системы, но это не работает.