0

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.

noob
  • 774
  • 1
  • 10
  • 23
Zen Koh
  • 1
  • 1
  • 1
  • 1
    can you show us the full code? where you are calling the destroy_window() function? – Nipun Thennakoon Dec 31 '18 at 10:24
  • `def create_monitor(monitor_url, monitor_counter_arg): website_url = monitor_url url = requests.get(website_url) # Could be better if I found a way to use access title from get_content without global url_html = url.content html_parser = etree.HTMLParser() tree = etree.fromstring(url_html, html_parser) title = tree.findtext( './/title' ) copyfile('default_config.py', f'config{monitor_counter_arg}.py') mod = importlib.import_module(f'config{monitor_counter_arg}') keys = mod.keys GUI.destroy_window()` – Zen Koh Dec 31 '18 at 10:26
  • You need to be careful when two modules imports each one in the other one. I can create error. I'm not sure if this your case, but it seems that is from your "note part" so look on this question to explore the idea [cyclic import](https://stackoverflow.com/questions/11698530/two-python-modules-require-each-others-contents-can-that-work) – Iulian Dec 31 '18 at 10:27
  • create_monitor is being called as logic.create_monitor() from GUI – Zen Koh Dec 31 '18 at 10:27
  • @lulian I'll look into that, Thanks – Zen Koh Dec 31 '18 at 10:28
  • 1
    @Zen Koh add all the code posted in the comment in the question's detail and indent it so it will be easier to look at it and so on. – Iulian Dec 31 '18 at 10:29

1 Answers1

1

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)
Laurens Deprost
  • 1,653
  • 5
  • 16