QObject没有属性'show'

时间:2013-10-18 15:56:48

标签: qt user-interface pyqt4 qtgui qtcore

我在Qt 4 Designer中设计了一个GUI,然后使用 pyuic4 将其编译为Python代码。

以下是编译的结果代码:

# -*- 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 个答案:

答案 0 :(得分: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()