2013-10-18 53 views
0

我在Qt 4 Designer中设计了一个GUI,然后使用pyuic4将它编译为Python代码。QObject没有属性'show'

以下是编译的结果代码:

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

# Form implementation generated from reading ui file 'Playmyflix.ui' 
# 
# Created: Fri Oct 18 21:22:19 2013 
#  by: PyQt4 UI code generator 4.9.3 
# 
# WARNING! All changes made in this file will be lost! 

from PyQt4 import QtCore, QtGui 

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

class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName(_fromUtf8("Dialog")) 
     Dialog.resize(737, 441) 
     Dialog.setAutoFillBackground(False) 
     Dialog.setStyleSheet(_fromUtf8("background-color:rgb(255, 255, 255);}")) 
     self.label = QtGui.QLabel(Dialog) 
     self.label.setGeometry(QtCore.QRect(120, 10, 471, 91)) 
     self.label.setText(_fromUtf8("")) 
     self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Playmyflix-LogoS.png"))) 
     self.label.setObjectName(_fromUtf8("label")) 
     self.KeyText = QtGui.QLineEdit(Dialog) 
     self.KeyText.setGeometry(QtCore.QRect(200, 140, 361, 41)) 
     self.KeyText.setStyleSheet(_fromUtf8("color: \"grey\";\n" 
"font: 75 18pt \"Cantarell\";")) 
     self.KeyText.setMaxLength(25) 
     self.KeyText.setAlignment(QtCore.Qt.AlignCenter) 
     self.KeyText.setObjectName(_fromUtf8("KeyText")) 
     self.PBar = QtGui.QProgressBar(Dialog) 
     self.PBar.setGeometry(QtCore.QRect(200, 370, 361, 31)) 
     self.PBar.setProperty("value", 0) 
     self.PBar.setObjectName(_fromUtf8("PBar")) 
     self.Report = QtGui.QTextBrowser(Dialog) 
     self.Report.setGeometry(QtCore.QRect(200, 240, 361, 111)) 
     self.Report.setObjectName(_fromUtf8("Report")) 
     self.GMov = QtGui.QPushButton(Dialog) 
     self.GMov.setGeometry(QtCore.QRect(310, 200, 131, 31)) 
     self.GMov.setStyleSheet(_fromUtf8("font: 75 16pt \"Cantarell\";")) 
     self.GMov.setObjectName(_fromUtf8("GMov")) 

     self.retranslateUi(Dialog) 
     QtCore.QObject.connect(self.GMov, QtCore.SIGNAL(_fromUtf8("clicked()")), Dialog.Operate) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

    def retranslateUi(self, Dialog): 
     Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8)) 
     self.KeyText.setText(QtGui.QApplication.translate("Dialog", "Enter Key", None, QtGui.QApplication.UnicodeUTF8)) 
     self.Report.setHtml(QtGui.QApplication.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:\'Cantarell\'; 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;\">Ready.......</p></body></html>", None, QtGui.QApplication.UnicodeUTF8)) 
     self.GMov.setText(QtGui.QApplication.translate("Dialog", "Get Movie", None, QtGui.QApplication.UnicodeUTF8)) 


# Added After Compile 
if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv) 
    var = Ui_Dialog() 
    var.show() 
    app.exec_loop() 

但是,当我试图使用来执行它:

python file.py 

我收到以下错误信息,不明白为什么:

Traceback (most recent call last): 
    File "file.py", line 66, in <module> 
    var.show() 
AttributeError: 'Ui_Dialog' object has no attribute 'show' 

回答

1

如果仔细观察由pyuic生成的代码,您将看到Ui_Dialog只是一个简单的python包装类,有两种方法。

唯一感兴趣的方法是setupUi,它采用您在Qt Designer中创建的顶级类的实例。

因此,运行代码,你需要做这样的事情:

widget = QtGui.QWidget() # or whatever your top-level class is 
ui = Ui_Dialog() 
ui.setupUi(widget) 
widget.show() 
相关问题