2015-03-13 174 views
0

我在PyQt中創建了一個有一個按鈕的主窗口。我希望使用該按鈕來顯示另一個窗口,並且主窗口應該關閉或隱藏。我寫了下面的代碼,但是單擊按鈕後主窗口沒有隱藏。在PyQt的一個窗口中顯示另一個窗口

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    _fromUtf8 = lambda s: s 


#Second window 
class Ui_Dialog(QtGui.QWidget): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName(_fromUtf8("Dialog")) 
     Dialog.setWindowModality(QtCore.Qt.WindowModal) 
     Dialog.resize(400, 300) 
     Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8)) 

     self.retranslateUi(Dialog) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

    def retranslateUi(self, Dialog): 
     pass 

#Main window 
class Ui_MainWindow(QtGui.QWidget): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(309, 148) 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(50, 30, 191, 71)) 
     self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Dialog", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     MainWindow.setCentralWidget(self.centralwidget) 

     self.retranslateUi(MainWindow) 

     self.pushButton.clicked.connect(self.showDial) 

     QtCore.QMetaObject.connectSlotsByName(MainWindow) 



    def retranslateUi(self, MainWindow): 
     pass 

    #function to display another form 
    def showDial(self): 
     Dialog = QtGui.QDialog() 
     u = Ui_Dialog() 
     u.setupUi(Dialog) 
     self.close() 
     Dialog.exec_() 
     #self.hide() 
     #self.close() 

if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    MainWindow = QtGui.QMainWindow() 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    sys.exit(app.exec_()) 
sys.exit(app.exec_()) 

回答

0

麻煩的是,在showDial,自我是UI對象,而不是窗口本身。就個人而言,我不喜歡單獨創建對象這樣(我想這是一個GUI設計師的靈感),但你可以對象本身存儲在setupUi並像這樣引用它:

class Ui_MainWindow(QtGui.QWidget): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(("MainWindow")) 
     MainWindow.resize(309, 148) 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(("centralwidget")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(50, 30, 191, 71)) 
     self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Open Dialog", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton.setObjectName(("pushButton")) 
     MainWindow.setCentralWidget(self.centralwidget) 

     self.pushButton.clicked.connect(self.showDial) 
     self.win = MainWindow 

     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    #function to display another form 
    def showDial(self): 
     Dialog = QtGui.QDialog() 
     u = Ui_Dialog() 
     u.setupUi(Dialog) 
     self.win.close() 
     Dialog.exec_() 
     #self.hide() 
     #self.close() 

(也注意到,你叫app.exec_()兩次)

一個簡單的替代你的模式:

MainWindow = Ui_MainWindow() 
    MainWindow.setupUi(MainWindow) #note that self and MainWindow are the same 

,並有Ui_MainWindow從QMainWindow的,而不是QWidget的繼承。

相關問題