运行下面发布的代码会带来一个 Dialog_01 QMainWindow,上面有一个按钮。 单击该按钮应该关闭此窗口( Dialog_01 )并将其替换为另一个窗口: Dialog_02 ,这实际上是第一个的副本。单击 Dialog_02 按钮将其关闭并重新打开 Dialog_01 。 代码运行时没有错误。但它确实遗漏了一些东西,因为Dialog_02即使在宣布时也没有显示出来。我错过了什么?
from PyQt4 import QtCore, QtGui
class Dialog_02(QtGui.QMainWindow):
def __init__(self):
super(Dialog_02, self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
Button_02.clicked.connect(self.callAnotherQMainWindow)
myBoxLayout.addWidget(Button_02)
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
def callAnotherQMainWindow(self):
print "This is supposed to call Dialog_01"
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(Dialog_01, self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
Button_01.clicked.connect(self.callAnotherQMainWindow)
myBoxLayout.addWidget(Button_01)
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
def callAnotherQMainWindow(self):
dialog_1.hide()
dialog_2 = Dialog_02()
dialog_2.resize(480,320)
dialog_2.show()
dialog_2.raise_()
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
错误在于使用dialog.hide()而不是dialog.close()。在显示第二个对话框之前,需要关闭(而不是隐藏)调用对话框。
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
class Dialog_02(QtGui.QMainWindow):
def __init__(self):
super(Dialog_02, self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
Button_02.clicked.connect(self.callAnotherQMainWindow)
myBoxLayout.addWidget(Button_02)
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.setWindowTitle('Dialog 02')
def callAnotherQMainWindow(self):
self.close()
self.dialog_01 = Dialog_01()
self.dialog_01.show()
self.dialog_01.raise_()
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(Dialog_01, self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
Button_01.clicked.connect(self.callAnotherQMainWindow)
myBoxLayout.addWidget(Button_01)
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.setWindowTitle('Dialog 01')
def callAnotherQMainWindow(self):
self.close()
self.dialog_02 = Dialog_02()
self.dialog_02.show()
self.dialog_02.raise_()
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
答案 0 :(得分:1)
Dialog_02
类中的修改以下方法:
def callAnotherQMainWindow(self)
self.close()
self.dialog_01 = Dialog_01()
self.dialog_01.show()
self.dialog_01.raise_()
在Dialog_01
类中修改以下方法:
def callAnotherQMainWindow(self)
self.close()
self.dialog_02 = Dialog_02()
self.dialog_02.show()
self.dialog_02.raise_()