1

我在 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'
4

1 回答 1

2

如果你仔细查看 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()
于 2013-10-18T17:07:05.803 回答