【问题标题】:QObject has no attribute 'show'QObject 没有属性“显示”
【发布时间】:2013-10-18 15:56:48
【问题描述】:

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

【问题讨论】:

    标签: qt user-interface pyqt4 qtgui qtcore


    【解决方案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()
    

    【讨论】:

    • 这应该是一个可以接受的答案。只是为了从未来增加一点清晰度-第一行是指顶级类是子小部件的任何内容(查看传递给的对象def.setupUi 找出那个对象是什么)。其次,您可能必须将 widget.show() 更改为 widget.exec_() 才能显示对话框。
    猜你喜欢
    • 2013-04-20
    • 2013-07-08
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多