Skip to content
Advertisement

Python Tkinter variable not updating in other thread

JavaScript

So I have a python script which accesses a website every minute, reads the page, and pings me if a certain data was placed on the website in that minute. I’m expanding this script to have a menu and a way to switch between different data points. The GUI and the script work well independently. However, the variable current_event_id does not update properly in my control_thread. I can mess with the GUI correctly; for instance, the icons change properly. However, the loop in run() does not reflect the change in current_event_id. I am hoping there is some simple workaround.

Advertisement

Answer

A classic pitfall with global variables in Python: In your change_dropdown function, you need to declare global curent_event_id before setting it, otherwise you are actually setting a local variable which is forgotten as soon as the function exits.

A simple demonstration:

JavaScript

This will print old x new y.

Also, AFAIK, the use of global outside a function has no effect in Python, since it only says: “Use or create this variable in the module variable namespace”. It is not needed as an additional declaration if you are introducing a variable in your module. So, in your case, if you just initialize your variable as:

JavaScript

This will have the same effect as the old

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