我想在两个对话框之间切换多次,但是我现在的代码只做了2次切换,然后就停止了。在问题(第二个对话框)
第二个对话到第一个对话(没有问题)
第一个对话框到第二个对话框(所以当我再次尝试转到第二个对话框时,代码停止运行)
我试着将可见性设置为True和False,而不是关闭对话框对话框.exec_()而不是对话框.exec()from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(633, 242)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(120, 70, 401, 81))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.page_2)
def page_2(self):
start_D2()
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog_1"))
self.pushButton.setText(_translate("Dialog", "Go to Second Page"))
class Ui_Dialog_2(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(633, 242)
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(120, 70, 401, 81))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.page_1)
def page_1(self):
start_D1()
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog_2"))
self.pushButton.setText(_translate("Dialog", "Go to First Page"))
def start_D2():
try:
Dialog.close()
ui = Ui_Dialog_2()
ui.setupUi(Dialog_2)
Dialog_2.exec()
except Exception as e:
print(e)
def start_D1():
try:
Dialog_2.close()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.exec()
except Exception as e:
print(e)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
Dialog_2 = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
我需要的是每次点击按钮时都要切换对话框,而不仅仅是一两次。在