Skip to content
Advertisement

Print variable from external file. NameError: name ‘x’ is not defined

I would like to get that: if I select item A in the combobox of main.py, then the variable example is printed as placeholder in phrase (both are located in the external file external.py). So I would like to simply get the phrase of external.py to be printed in the textbox in main.py.

enter image description here

The problem is that phrase is in an external file and does not recognize x.get() in the condition, because x.get() is in the main file main.py, so rightly it is undefined and I get the error:

JavaScript

external.py

JavaScript

main.py

JavaScript

IMPORTANT: I need the condition in the same line as the phrase and of the same code structure as much as possible to mine, because the code is part of my small personal project and if I change the code structure then nothing will work anymore.

Advertisement

Answer

Put the code in external.py into a function, and call that from your write() function. Use x.get() in the main program, so that the module isn’t dependent on your variable names or the fact that the main program uses Tkinter.

external.py:

JavaScript

main.py:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement