Zen Koh
Zen Koh

Reputation: 1

NameError: name 'test' is not defined , when trying to run a function in an imported module/script Python 3

I'm keeping a GUI script and a logic script separate to keep things simpler (using VSCode) and when I run the GUI script I'm calling a function in a logic script:GUI.destroy_window() which in turn calls a function in GUI:

def destroy_window():
    test.destroy()

However, even though I previously defined test in GUI when I ran it, I get this:

line 43, in create_monitor
    GUI.destroy_window()
 line 30, in destroy_window
    test.destroy()
NameError: name 'test' is not defined

Note: I have imported both scripts into each other and I made test global.

Upvotes: 0

Views: 3035

Answers (1)

Laurens Deprost
Laurens Deprost

Reputation: 1691

The variable test is not known in the scope of the function destroy_window.

Try to pass the object to the function instead:

def destroy_window(window):
    window.destroy()

destroy_window(test)

Upvotes: 1

Related Questions