2014-05-17 67 views
0

我有兩個QMainWindows,我試圖從另一個調用一個。該窗口打開,但它是空的,直到父窗口關閉纔會響應。空的QMainWindow,PyQt

我的代碼:

from PyQt4 import QtCore, QtGui 

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

class Ui_MainWindow(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QMainWindow.__init__(self,None) 

    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(184, 165) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(40, 40, 75, 23)) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 184, 21)) 
     self.menubar.setObjectName(_fromUtf8("menubar")) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     self.statusbar.setObjectName(_fromUtf8("statusbar")) 
     MainWindow.setStatusBar(self.statusbar) 

     self.retranslateUi(MainWindow) 
     QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()"),self.gone) 
     QtCore.QMetaObject.connectSlotsByName(MainWindow) 

    def retranslateUi(self, MainWindow): 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8)) 

    def gone(self): 
     mainwindow = QtGui.QMainWindow() 
     self.mw2 = Ui_MainWindow1() 
     self.mw2.setupUi(mainwindow) 
     self.mw2.show() 

class Ui_MainWindow1(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QMainWindow.__init__(self,None) 

    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(170, 175) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.label = QtGui.QLabel(self.centralwidget) 
     self.label.setGeometry(QtCore.QRect(40, 60, 101, 16)) 
     self.label.setObjectName(_fromUtf8("label")) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 170, 21)) 
     self.menubar.setObjectName(_fromUtf8("menubar")) 
     MainWindow.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(MainWindow) 
     self.statusbar.setObjectName(_fromUtf8("statusbar")) 
     MainWindow.setStatusBar(self.statusbar) 

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

    def retranslateUi(self, MainWindow): 
     MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label.setText(QtGui.QApplication.translate("MainWindow", "This is second", None, QtGui.QApplication.UnicodeUTF8)) 

def startmain(): 
    app = QtGui.QApplication(sys.argv) 
    MainWindow = QtGui.QMainWindow() # <-- Instantiate QMainWindow object. 
    ui = Ui_MainWindow() 
    ui.setupUi(MainWindow) 
    MainWindow.show() 
    app.exec_() 

if __name__ == "__main__": 
    import sys 
    startmain() 

回答

1

我不明白爲什麼要創建QMainWindow對象時,你Ui_MainWindowUi_MainWindow1類的子類QMainWindow。基本上,你有四個主窗口,當你只需要兩個。

而是在其中創建QMainWindow叫做MainWindow,並設置東西,用self代替MainWindow本身每個窗口中設置的。下面的工作對我來說,第一個窗口有一個按鈕,並點擊它打開第二個帶有標籤的地方:

from PyQt4 import QtCore, QtGui 

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

class Ui_MainWindow(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QMainWindow.__init__(self,None) 

    def setupUi(self): 
     self.setObjectName(_fromUtf8("self")) 
     self.resize(184, 165) 
     self.centralwidget = QtGui.QWidget(self) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(40, 40, 75, 23)) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(self) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 184, 21)) 
     self.menubar.setObjectName(_fromUtf8("menubar")) 
     self.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(self) 
     self.statusbar.setObjectName(_fromUtf8("statusbar")) 
     self.setStatusBar(self.statusbar) 

     self.retranslateUi() 
     QtCore.QObject.connect(self.pushButton , QtCore.SIGNAL("clicked()"),self.gone) 
     QtCore.QMetaObject.connectSlotsByName(self) 

    def retranslateUi(self): 
     self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "PushButton", None, QtGui.QApplication.UnicodeUTF8)) 

    def gone(self): 
     self.mw2 = Ui_MainWindow1() 
     self.mw2.setupUi() 
     self.mw2.show() 

class Ui_MainWindow1(QtGui.QMainWindow): 
    def __init__(self): 
     QtGui.QMainWindow.__init__(self,None) 

    def setupUi(self): 
     self.setObjectName(_fromUtf8("self")) 
     self.resize(170, 175) 
     self.centralwidget = QtGui.QWidget(self) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.label = QtGui.QLabel(self.centralwidget) 
     self.label.setGeometry(QtCore.QRect(40, 60, 101, 16)) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(self) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 170, 21)) 
     self.menubar.setObjectName(_fromUtf8("menubar")) 
     self.setMenuBar(self.menubar) 
     self.statusbar = QtGui.QStatusBar(self) 
     self.statusbar.setObjectName(_fromUtf8("statusbar")) 
     self.setStatusBar(self.statusbar) 

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

    def retranslateUi(self): 
     self.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) 
     self.label.setText(QtGui.QApplication.translate("MainWindow", "This is second", None, QtGui.QApplication.UnicodeUTF8)) 

def startmain(): 
    app = QtGui.QApplication(sys.argv) 
    ui = Ui_MainWindow() 
    ui.setupUi() 
    ui.show() 
    app.exec_() 

if __name__ == "__main__": 
    import sys 
    startmain() 
+0

Thanx Luke,我只是想把孩子窗口放在父母的後面並且在父母關閉之前無效 – TheCreator232