Skip to content
Advertisement

Qt Framework, PyQt5 and AttributeError: ‘MyApp’ object has no attribute ‘myAttribute’

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“):

JavaScript

After that I added a new python file where I put my code; this is what i wrote (file name “myapp.py“):

JavaScript

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:

JavaScript

But the compiler shows alway this error:

AttributeError: ‘MyApp’ object has no attribute ‘time’

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,

Andrea

Advertisement

Answer

You’re not far off.

The MyApp class needs to inherit QMainWindow, and you don’t need to use the time module. Try something like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement