2013-05-30 136 views
1

我有兩個.ui文件,我用pyuic轉換爲python文件。我現在想要做的是從另一個開放。麻煩從另一個python(2.7)文件打開Qt4 python文件

代碼的主窗口:第二個窗口

# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'text.ui' 
# 
# Created: Thu May 30 11:40:12 2013 
#  by: PyQt4 UI code generator 4.10 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt4 import QtCore, QtGui 
from hi import Ui_Dialog 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName(_fromUtf8("MainWindow")) 
     MainWindow.resize(800, 600) 
     self.centralwidget = QtGui.QWidget(MainWindow) 
     self.centralwidget.setObjectName(_fromUtf8("centralwidget")) 
     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setGeometry(QtCore.QRect(10, 520, 98, 27)) 

     ##Connects the clicked event to the open function 
     self.pushButton.clicked.connect(self.open) 


     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.textEdit = QtGui.QTextEdit(self.centralwidget) 
     self.textEdit.setGeometry(QtCore.QRect(60, 60, 541, 121)) 
     self.textEdit.setObjectName(_fromUtf8("textEdit")) 
     MainWindow.setCentralWidget(self.centralwidget) 
     self.menubar = QtGui.QMenuBar(MainWindow) 
     self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25)) 
     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(_translate("MainWindow", "MainWindow", None)) 
     self.pushButton.setText(_translate("MainWindow", "PushButton", None)) 

    def open(self): 



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

代碼(對話):

# -*- coding: utf-8 -*- 

# Form implementation generated from reading ui file 'hi.ui' 
# 
# Created: Thu May 30 11:41:17 2013 
#  by: PyQt4 UI code generator 4.10 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt4 import QtCore, QtGui 

try: 
    _fromUtf8 = QtCore.QString.fromUtf8 
except AttributeError: 
    def _fromUtf8(s): 
     return s 

try: 
    _encoding = QtGui.QApplication.UnicodeUTF8 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig, _encoding) 
except AttributeError: 
    def _translate(context, text, disambig): 
     return QtGui.QApplication.translate(context, text, disambig) 

class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName(_fromUtf8("Dialog")) 
     Dialog.resize(400, 300) 
     self.buttonBox = QtGui.QDialogButtonBox(Dialog) 
     self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32)) 
     self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 
     self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) 
     self.buttonBox.setObjectName(_fromUtf8("buttonBox")) 
     self.tabWidget = QtGui.QTabWidget(Dialog) 
     self.tabWidget.setGeometry(QtCore.QRect(0, 10, 391, 161)) 
     self.tabWidget.setObjectName(_fromUtf8("tabWidget")) 
     self.tab = QtGui.QWidget() 
     self.tab.setObjectName(_fromUtf8("tab")) 
     self.textEdit = QtGui.QTextEdit(self.tab) 
     self.textEdit.setGeometry(QtCore.QRect(130, 40, 104, 78)) 
     self.textEdit.setObjectName(_fromUtf8("textEdit")) 
     self.tabWidget.addTab(self.tab, _fromUtf8("")) 
     self.tab_2 = QtGui.QWidget() 
     self.tab_2.setObjectName(_fromUtf8("tab_2")) 
     self.tabWidget.addTab(self.tab_2, _fromUtf8("")) 

     self.retranslateUi(Dialog) 
     QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept) 
     QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

    def retranslateUi(self, Dialog): 
     Dialog.setWindowTitle(_translate("Dialog", "Dialog", None)) 
     self.textEdit.setHtml(_translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n" 
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" 
"p, li { white-space: pre-wrap; }\n" 
"</style></head><body style=\" font-family:\'Ubuntu\'; font-size:11pt; font-weight:400; font-style:normal;\">\n" 
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">hi</p></body></html>", None)) 
     self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("Dialog", "Tab 1", None)) 
     self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("Dialog", "Tab 2", None)) 


if __name__ == "__main__": 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    Dialog = QtGui.QDialog() 
    ui = Ui_Dialog() 
    ui.setupUi(Dialog) 
    Dialog.show() 
    sys.exit(app.exec_()) 

我的猜測是,我將不得不進口的其他文件和呼叫一些方法使其顯示。有沒有辦法這樣做:

secondWindow = Ui_Dialog 
secondWindow.show() 

或沿着這些線?我試過類似的東西,但它說沒有方法show()。那是因爲班級的構造方式嗎?

我甚至試過這樣的事情:

def nom(): 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    Dialog = QtGui.QDialog() 
    ui = Ui_Dialog() 
    ui.setupUi(Dialog) 
    Dialog.show() 
    sys.exit(app.exec_()) 

我的想法是,如果if __name__=="main":下的代碼被啓動時執行的,爲什麼我不能只是把那另外一個函數。

回答

1

一個簡單的工作示例是這樣的:

import sys 
from PyQt4 import QtGui 
from mainwindow import Ui_MainWindow 
from dialog import Ui_Dialog 

class MainWindow(QtGui.QMainWindow, Ui_MainWindow): 
    def __init__(self): 
     QtGui.QMainWindow.__init__(self) 
     self.setupUi(self) 
     self.pushButton.clicked.connect(self.show_dialog) 
    def show_dialog(self): 
     dialog = Dialog() 
     ret = dialog.exec_() 
     if ret == QtGui.QDialog.Accepted: 
      self.textEdit.append('accepted') 
     else: 
      self.textEdit.append('rejected') 

class Dialog(QtGui.QDialog, Ui_Dialog): 
    def __init__(self): 
     QtGui.QMainWindow.__init__(self) 
     self.setupUi(self) 

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

,如果你命名你的第一個文件mainwindow.py第二個文件dialog.py工作。 第一類從Ui_MainWindow繼承,另一個從Ui_Dialog繼承,因此它們從導入的文件中獲取代碼。然後他們呼叫setupUi(self)它「生成」窗口。

if __name__ ...事件的底部,它實例化兩個類,然後show()將工作並顯示窗口。

如果您使用pyuic生成代碼,然後更改代碼,那麼如果修改.ui-文件並重新生成python代碼,它將被簡單地覆蓋!因此,您應該始終創建一個「主」文件並將GUI導入其中。

如果你從你的第一個文件刪除from hi import Ui_Dialogdef open(self):

自己的函數代碼只會工作的open(這應該得到因爲內建open的另一個名字)應該進入主文件!

編輯:更改了代碼,以便在點擊「PhusButton」時顯示對話框。
要「開始」一個對話框,您需要使用其方法exec_。在上面的代碼中,clicked按鈕的信號連接到show_dialog方法。這個方法在每次點擊按鈕後被調用。然後它執行對話框,並在對話框關閉時寫入文本框ok取消

+0

我真正想要做的是能夠從mainwindow調用對話框的qt-py文件。我會怎麼做呢? 感謝您的這個小片段! [我這樣試過:](http://pastie.org/7984613) – Zeratas

+0

好吧,我已經更改了代碼,所以它會在每次點擊按鈕時打開對話框。 – TobiMarg

+0

Whooohooo。我得到你所做的和我搞砸的地方謝謝! – Zeratas

相關問題