Python threading issue: BUTTON.when_pressed event yeilds "RuntimeError: main thread is not in main loop"

Thread Starter

ErnieM

Joined Apr 24, 2011
8,415
I am working on a multi window game app where I need to handle some button presses. One thing I would like to do is to display a massage box over one of these windows.

However, the tkinter GUI thread is separate from the button event thread. Attempting to access any GUI objects from the button event issues a runtime error.

How would one synchronize these threads? How would my nominal main thread know when this button has been pressed?
 

nsaspook

Joined Aug 27, 2009
16,249
Python threads, block until event. A thread is just a lightweight process that shares the process environment. It emulated parallelism of processing using the common abstraction of threads. There are several methods queues, shared memory, etc ...
https://medium.com/@surve.aasim/thread-synchronization-in-python-using-queue-1be0df535a66
https://medium.com/@surve.aasim/thread-synchronization-in-python-using-event-dbe1752eb5c7
https://docs.python.org/3/library/threading.html

Not a Python guy but the methods are the same with any modern language.
 

Futurist

Joined Apr 8, 2025
720
I am working on a multi window game app where I need to handle some button presses. One thing I would like to do is to display a massage box over one of these windows.

However, the tkinter GUI thread is separate from the button event thread. Attempting to access any GUI objects from the button event issues a runtime error.

How would one synchronize these threads? How would my nominal main thread know when this button has been pressed?
This might be similar to windows then. On Windows there's a single dedicated GUI thread, it services UI events and manages UI element IO.

Other "user" threads that might do work for a user, like pulling data from a DB and updating a tabular control to display the data.

On Windows you'll get a cross thread exception if a user's thread tries to update a UI element, because the GUI thread might conflict with that.

So there are ways to delegate UI updates, one can ask that the operation be delegated to the GUI thread, that does the update and returns and the user's thread then resumes.

This will explain better - Dispatcher in WPF.

Basically any manipulation of GUI elements must be done on the GUI thread, its a rule and so we can "hand off" updates in such a way that the GUI thread does them on behalf of the user's thread.

I suspect Python is very similar, in fact if Python is running on Windows then ultimately it will be Windows that manages the UI. Tkinter relies on Tcl/Tk for GUI operations and that Tcl/Tk layer takes care of the OS, it "knows" which OS its on and how to do GUI on that OS, the Tcl/Tk layer is written in C.

Take a look at .Net MAUI that lets you write a sophisticated GUI in C# that runs on Mac, Windows, iPhone, Android etc, it will give you more sheer performance and power than Python can.
 
Last edited:

Ya’akov

Joined Jan 27, 2019
10,226
Python threads, block until event. A thread is just a lightweight process that shares the process environment. It emulated parallelism of processing using the common abstraction of threads. There are several methods queues, shared memory, etc ...
https://medium.com/@surve.aasim/thread-synchronization-in-python-using-queue-1be0df535a66
https://medium.com/@surve.aasim/thread-synchronization-in-python-using-event-dbe1752eb5c7
https://docs.python.org/3/library/threading.html

Not a Python guy but the methods are the same with any modern language.
Working (not-quite-but-almost-blind) I would make a SWAG that using a work queue is what he wants.
 
Top