Qt 框架、PyQt5 和 AttributeError:“MyApp"对象没有属性“myAttribute" [英] Qt Framework, PyQt5 and AttributeError: 'MyApp' object has no attribute 'myAttribute'
问题描述
上周我开始学习 Python 并开发了一些命令行应用程序.现在我想用 GUI 开发应用程序.我在互联网上搜索,发现了一个符合我需求的项目:Qt Project (http://qt-project.org) 和 PyQt(http://www.riverbankcomputing.com/software/pyqt/intro).我在 Mac OS X 10.10 和 python 2.7.6 上安装了 Qt 5.3.2 Open Source、SIP 4.16.4、PyQt5 5.3.2.在安装 Qt 和 PyQt 时遇到了一些麻烦,最后我设法让它们工作.如果我从 PyQt 示例文件夹中打开示例项目,gui 出现时没有任何问题.所以我用 Qt Creator 创建了我的 GUI,然后我使用 pyuic5 生成了 python 代码.这是 pyuic5 创建的(文件名myapp_auto.py"):
Last week I started to learn Python and I developed some command line apps. Now I would like to develop apps with GUI. I searched in internet and I found a project that fits my needs: Qt Project (http://qt-project.org) and PyQt (http://www.riverbankcomputing.com/software/pyqt/intro). I installed Qt 5.3.2 Open Source, SIP 4.16.4, PyQt5 5.3.2 on Mac OS X 10.10 and python 2.7.6. After some troubles on installing Qt and PyQt, finally I managed to make them work. If I open example projects from PyQt example folder the gui appears without any problems. So I created my GUI with Qt Creator and then I used pyuic5 to generate python code. This is what pyuic5 created (file name "myapp_auto.py"):
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/Users/andrea/Developer/Qt/mainwindow.ui'
#
# Created: Mon Nov 17 14:20:14 2014
# by: PyQt5 UI code generator 5.3.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.ok = QtWidgets.QPushButton(self.centralWidget)
self.ok.setGeometry(QtCore.QRect(140, 120, 115, 32))
self.ok.setAccessibleName("")
self.ok.setObjectName("ok")
self.text = QtWidgets.QLabel(self.centralWidget)
self.text.setGeometry(QtCore.QRect(100, 70, 181, 16))
self.text.setAccessibleName("")
self.text.setAlignment(QtCore.Qt.AlignCenter)
self.text.setObjectName("text")
self.time = QtWidgets.QDateTimeEdit(self.centralWidget)
self.time.setGeometry(QtCore.QRect(100, 180, 194, 24))
self.time.setAccessibleName("")
self.time.setObjectName("time")
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 22))
self.menuBar.setObjectName("menuBar")
self.menuMyApp = QtWidgets.QMenu(self.menuBar)
self.menuMyApp.setObjectName("menuMyApp")
self.menuEdit = QtWidgets.QMenu(self.menuBar)
self.menuEdit.setObjectName("menuEdit")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.menuBar.addAction(self.menuMyApp.menuAction())
self.menuBar.addAction(self.menuEdit.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.ok.setText(_translate("MainWindow", "Ok"))
self.text.setText(_translate("MainWindow", "I\'m a GUI"))
self.menuMyApp.setTitle(_translate("MainWindow", "MyApp"))
self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
之后,我添加了一个新的 python 文件来放置我的代码;这是我写的(文件名myapp.py"):
After that I added a new python file where I put my code; this is what i wrote (file name "myapp.py"):
#!/usr/bin/env python
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from myapp_auto import Ui_MainWindow
import sys
import time
class MyApp(Ui_MainWindow):
parse_triggered = pyqtSignal()
def __init__(self, parent=None, name=None):
Ui_MainWindow.__init__(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = MyApp()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
然后,我运行 myapp.py 并验证所有 GUI 元素似乎都在它们应该在的位置.好吧……现在我的问题来了:我尝试使用代码访问 MainWindow 中的time"元素,从而修改 init def:
Then, I run myapp.py and I verified that all GUI elements appeared to be where they should be. Well...now arrive my issue: I tried to access with code the "time" element in MainWindow modifying init def thus:
def __init__(self, parent=None, name=None):
Ui_MainWindow.__init__(self)
# Set the date to now
now = QDateTime()
now.setTime_t(int(time.time()))
self.time.setDateTime(now)
但是编译器总是显示这个错误:
But the compiler shows alway this error:
AttributeError: 'MyApp' 对象没有属性 'time'
AttributeError: 'MyApp' object has no attribute 'time'
即使我尝试访问任何其他元素(ok"、text")也会发生这种情况.肯定会是一个愚蠢的错误,但我无法弄清楚我哪里出错了.谢谢大家!祝你有美好的一天,
This happen even if I try to access any other element ("ok", "text"). Will surely be a stupid mistake but I just can not figure out where I went wrong. Thank you all guys! Have a good day,
安德里亚
推荐答案
离你不远了.
MyApp
类需要继承QMainWindow
,不需要使用time
模块.尝试这样的事情:
The MyApp
class needs to inherit QMainWindow
, and you don't need to use the time
module. Try something like this:
class MyApp(QMainWindow, Ui_MainWindow):
parse_triggered = pyqtSignal()
def __init__(self, parent=None, name=None):
super(MyApp, self).__init__(parent)
self.setupUi(self)
# Set the date to now
self.time.setDateTime(QDateTime.currentDateTime())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
这篇关于Qt 框架、PyQt5 和 AttributeError:“MyApp"对象没有属性“myAttribute"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!