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??

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book


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!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Signals, Slots and Events: NameError: name 'QMainWindow' is not defined was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.