PyQT5:如何连续打开和关闭3windows
PyQT5: How to open and close 3 windows consecutively
我的 GUI 过程中有问题。我有 3 window 这是主要的 window -> 2nd window -> 3rd window 然后退出。单击转到 2nd window 的按钮时效果很好,但如果我单击转到 window 3 的按钮,它就会崩溃。
全程碾压不说报错
这是我的第一个代码 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_())
这是第二个的代码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_())
这是第三个的代码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_())
有人能告诉我为什么单击按钮时进程在第 2 个 window 崩溃,为什么它不能继续到第 3 个 window?
试一试:
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_())
我的 GUI 过程中有问题。我有 3 window 这是主要的 window -> 2nd window -> 3rd window 然后退出。单击转到 2nd window 的按钮时效果很好,但如果我单击转到 window 3 的按钮,它就会崩溃。
全程碾压不说报错
这是我的第一个代码 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_())
这是第二个的代码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_())
这是第三个的代码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_())
有人能告诉我为什么单击按钮时进程在第 2 个 window 崩溃,为什么它不能继续到第 3 个 window?
试一试:
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_())