Signals, Slots and Events: NameError: name 'QMainWindow' is not defined

Heads up! You've already completed this tutorial.

Nigel_Trewartha | 2020-05-07 15:08:46 UTC | #1

I am using WIngIDE Pro version 7.2.2.2 on a WIn 10 64bit Pro. and am using the code under the "Signals. Slots and Events.

https://www.pythonguis.com/courses/start/signals-slots-events/#jc8dc3b580-deef-49f8-bf79-a2e67e6fa50a

I get this error;

python
Traceback (most recent call last):
File "D:\Eigene Dateien\PYQT5_Sources\test4-signals and slots1.py", line 5, in <module>
class MainWindow(QMainWindow):
NameError: name 'QMainWindow' is not defined

Process terminated with an exit code of 1

I even changed to code to include this:

python
import sys
from PyQt5.QtWidgets import QApplication
import PyQt5

So I appear to be doing something very wrong. Can I get help here please??


martin | 2020-05-07 15:10:11 UTC | #2

Hi Nigel, welcome to the site!

This is because you don't have an import for QMainWindow. You can either import QMainWindow itself or you can import QtWidgets and access QMainWindow through there.

python
class MainWindow(QtWidgets.QMainWindow):

or

python
from PyQt5.QtWidgets import QApplication, QMainWindow

Will update this tutorial to include the full code to start with as this is very confusing.


Nigel_Trewartha | 2020-05-07 15:10:31 UTC | #3

Thany you for your reply. I copied your code. That does have a Mainwindow. Perhaps an idea to include this in your code.

I also cannot find "Load up a fresh copy of MyApp_window.py and save it under a new name for this section". I assume you mean one of the exercises in the last section..


Nigel_Trewartha | 2020-05-07 15:11:15 UTC | #4

I added the import from PyQt5.QtWidgets import QApplication, QMainWindow

But when I start the programme it seems to do nothing. Tried it under WingIDE pro and cmd -> python "filename"

Since I just copied and pasted your programme snippet and added from PyQt5.QtWidgets import QApplication, QMainWindow . I do not see what I am doing wrong.


martin | 2020-05-18 11:53:34 UTC | #6

[quote="Nigel_Trewartha, post:3, topic:32"] Perhaps an idea to include this in your code. [/quote]

I've added the base code to the top of the tutorial, should make it clearer -- thanks for the feedback!


Over 10,000 developers have bought Create GUI Applications with Python & Qt!

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!

For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak