PyQT:如何从另一个Dialog(QMainWindow)中调用Dialog

时间:2014-03-06 03:18:42

标签: python pyqt qtgui

运行下面发布的代码会带来一个 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_())

1 个答案:

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