Python error: global declared variable is not declared in the global scope

Andrew T

I am quite new to python, and I tried to make a simple GUI program. But, I got into a "problem", exactly a warning, that says: 'm' is not defined in the global scope (Python(variable-not-defined-globally)).

I know that you need to declare a var global inside a function if you want to access it outside that function scope. Although I don't use this new created variable outside the function, if I don't declare it global, my program only shows the GUI for a fraction of a second, then it closes it.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

def show():
    global m
    m = QWidget()
    m.setWindowTitle("Testing this app")
    m.show()

MYAPP = QApplication(sys.argv)
show()
MYAPP.exec_()

Could you explain why is this happening? Thanks in advance!

Tom Lubenow

global tells python to look for a variable with this name in the global namespace and include it in the local namespace. This means it must exist in the global namespace first.

import sys
from PyQt5.QtWidgets import QApplication, QWidget

m = None  # variable must exist in global namespace first

def show():
    global m  # this creates a local m that is linked to the global m
    m = QWidget()
    m.setWindowTitle("Testing this app")
    m.show()

MYAPP = QApplication(sys.argv)
show()
MYAPP.exec_()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How is a global variable declared?

Is a function declared in the global scope a closure?

Error: Variable Not declared in this scope

When exactly is a thread_local variable declared at global scope initialized?

In JavaScript; does a let variable declared outside of a block get a global scope?

Javascript: While the variable is declared in global scope, it remains undefined inside the function

I declared my variable with let but it is still not in global scope

Why can't I access a declared variable in the global scope

TypeScript global variable is not defined, declared in declare global{}

variable was not declared in this scope gcc error

Visibility of global variable declared in imported module

global variable not recognised when declared in procedure

Use Global Variable In Same File As It's Declared?

Variable declared and assigned at global scope is undefined when called by a nested return function

functions declared on global object

Python Declared Global Class List is Creating the Error: Unknown global name...is not defined

Variable is not declared in the scope error in c++?

How to fix the 'variable' was not declared in this scope error?

How to check if function is declared in global scope at compile time

Scope of variable declared in condition

Scope of try declared variable

Modelica variable not declared in this scope

isn't the variable declared outside with let global variable too?

Why must a variable be declared a global variable before it gets assigned?

Javascript variable declared in if statement wont change the global variable

eslint shows error when I use my previously declared global variable

error: '_hypot' was not declared in this scope

error: 'qmlRegisterType' was not declared in this scope

error: 'strdup' was not declared in this scope