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 on a WIn 10 64bit Pro. and am using the code under the "Signals. Slots and Events.

I get this error;

Traceback (most recent call last):
File "D:\Eigene Dateien\PYQT5_Sources\test4-signals and", 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:

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.

class MainWindow(QtWidgets.QMainWindow):


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

1:1 Coaching & Tutoring for your Python GUIs project
Martin Fitzpatrick Python GUIs Coaching & Training
60 mins ($195) More info

1:1 Python GUIs Coaching & Training

Comprehensive code reviewBugfixes & improvements • Maintainability advice and architecture improvements • Design and usability assessment • Suggestions and tips to expand your knowledgePackaging and distribution help for Windows, Mac & Linux • Find out more.

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.