2013-07-06 36 views
0

我看過這個video,然後我成功創建了hello.py。但是我運行hello.py,不顯示錶單。我需要幫助。我從來沒有犯過任何錯誤。 在qt設計器中創建接口後,我創建了hello.py腳本。 「C:\ Python27 \ LIB \站點包\ PyQt4的\ pyuic4.bat」 hello.ui -o hello.py 的代碼低於:python qt4不能正常工作

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_Form(object): 
    def setupUi(self, Form): 
     Form.setObjectName(_fromUtf8("Form")) 
     Form.resize(509, 312) 
     self.gridLayout = QtGui.QGridLayout(Form) 
     self.gridLayout.setObjectName(_fromUtf8("gridLayout")) 
     self.verticalLayout = QtGui.QVBoxLayout() 
     self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) 
     self.label = QtGui.QLabel(Form) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.verticalLayout.addWidget(self.label) 
     self.horizontalLayout = QtGui.QHBoxLayout() 
     self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 
     self.lineEdit = QtGui.QLineEdit(Form) 
     self.lineEdit.setObjectName(_fromUtf8("lineEdit")) 
     self.horizontalLayout.addWidget(self.lineEdit) 
     self.pushButton = QtGui.QPushButton(Form) 
     self.pushButton.setObjectName(_fromUtf8("pushButton")) 
     self.horizontalLayout.addWidget(self.pushButton) 
     self.verticalLayout.addLayout(self.horizontalLayout) 
     self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1) 

     self.retranslateUi(Form) 
     QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.label.clear) 
     QtCore.QObject.connect(self.lineEdit, QtCore.SIGNAL(_fromUtf8("textChanged(QString)")), self.label.setText) 
     QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.lineEdit.clear) 
     QtCore.QMetaObject.connectSlotsByName(Form) 

    def retranslateUi(self, Form): 
     Form.setWindowTitle(_translate("Form", "Form", None)) 
     self.label.setText(_translate("Form", "Hello World", None)) 
     self.pushButton.setText(_translate("Form", "PushButton", None)) 

回答

2

您需要實例化對象,並設置它。在當前代碼的底部添加此代碼將顯示錶單。

import sys 

class MyForm(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QWidget.__init__(self, parent) 
     self.ui = Ui_Form() 
     self.ui.setupUi(self) 
    def execute_event(self): 
     pass 
    def execute_all_event(self): 
     pass 
    def reload_event(self): 
     pass 

if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    myapp = MyForm() 
    myapp.show() 
    sys.exit(app.exec_()) 

本答案來自於similar question