2014-05-14 172 views
1

好吧,這是一個幾天來一直困擾我的問題。我試圖從MainWindow打開一個Ui_form。出於某種原因,子窗口會立即打開和關閉。從PyQt的主窗口中打開Ui_Form

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

    def setupUi(self, MainWindow): 
     # Some Gui Code 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(406, 234) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget) 
     self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) 
     self.horizontalLayout = QtGui.QHBoxLayout() 
     self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 
     self.listView = QtGui.QListView(self.centralwidget) 
     self.listView.setObjectName(_fromUtf8("listView")) 
     self.horizontalLayout.addWidget(self.listView) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.horizontalLayout.addWidget(self.pushButton) 
     self.verticalLayout.addLayout(self.horizontalLayout) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 406, 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.NewLog) 
     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", "New Log", None, QtGui.QApplication.UnicodeUTF8)) 



    def NewLog(self): 
     app = QtGui.QWidget() 
     myapp = new_application_log.Ui_Form() 
     myapp.setupUi(app) 
     myapp.show() 
     #app.exec_() 

class Ui_Form(QtGui.QWidget): 
    def __init__(self,parent = None): 
     QtGui.QWidget.__init__(self,parent) 

    def setupUi(self, Form): 
     # Some Gui Code. 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(312, 269) 
     self.verticalLayout = QtGui.QVBoxLayout(Form) 
     self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) 
     self.listView = QtGui.QListView(Form) 
     self.listView.setObjectName(_fromUtf8("listView")) 
     self.verticalLayout.addWidget(self.listView) 

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

    def retranslateUi(self, Form): 
     Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) 

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

看起來這之前只是一個範圍問題。你能發佈一些可用的代碼嗎?您可以嘗試創建變量,將您的新UI窗體設置爲主窗口類中的某處,這樣,當NewLog函數作爲快速修復而退出時,它不會超出範圍 – user3591723

+0

儘管這並不完全必要爲什麼我想看看你是如何使用該代碼。現在它實際上並沒有對新表單做任何事情 – user3591723

+0

@ user3591723:我已經添加了一些可能對您有幫助的代碼,但它不是整個代碼。希望能幫助到你。 – TheCreator232

回答

1

最簡單的解決方案是你的newLog與在Ui_mainwindow

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

     self.form = None 

定義在你的代碼中self.form改變這種

def NewLog(self): 
    app = QtGui.QWidget() 
    self.form = Ui_Form() 
    self.form.setupUi(app) 
    self.form.show() 

,你在裏面定義myapp = Ui_form()變量的newLog函數,然後它將超出範圍並在函數退出時被正確刪除秒。如果您想以某種方式包含某些功能的結束功能,您在技術上不需要將self.form置於Ui_mainWindow__init__()中。舉個例子,比如說,你有一個可以返回的表單,需要點擊按鈕才能關閉,然後你可以按照你的要求去做,但是因爲你只是一個小部件,所以你需要讓它在功能。


編輯:例如,該代碼將運行得很好由於上述原因 - 你必須按一下按鈕的功能退出

def NewLog(self, text='I am open!', buttons=QtGui.QMessageBox.Ok): 
    errorBox = QtGui.QMessageBox() 
    errorBox.setText(text) 
    errorBox.setStandardButtons(buttons) 
    return errorBox.exec_() 
+0

謝謝,您的回答有幫助。 – TheCreator232

+0

很高興我能幫忙:) – user3591723