如何通过按钮打开另一个gui?

时间:2016-06-19 07:00:41

标签: python python-2.7 qt python-3.x

我是由qt设计师制作的几个gui,想通过按钮连接它们

如果我在主gui中按下按钮,我想打开第二个gui

SecDialog是我的第二个gui,MainWindow是主要的gui

有什么问题?

import sys
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from secdialog import Ui_SecDialog

class SecDialog(QtGui.QDialog, Ui_SecDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.handleMainWindow)

    def handleMainWindow(self):
        window = SecDialog(self)
        window.show

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()        
    ex.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您需要在window.show之后添加左括号和右括号:

import sys
from PyQt4 import QtCore, QtGui
from mainwindow import Ui_MainWindow
from secdialog import Ui_SecDialog

class SecDialog(QtGui.QDialog, Ui_SecDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setupUi(self)

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.handleMainWindow)

    def handleMainWindow(self):
        window = SecDialog(self)
        window.show() # <-- show is a method, therefore you need parentheses after it to call it

if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv)
    ex = MainWindow()        
    ex.show()
    sys.exit(app.exec_())