Program freezing during the execution of a function in Tkinter

Tkinter is single threaded. Screen updates happen on each trip through the event loop. Any time you have a long running command you are preventing the event loop from completing an iteration, thus preventing the processing of events, thus preventing redraws.

Your only solution is a) use a thread for the long running command, b) use a process for the long running command, or c) break the command up into small chunks that each can be run in a few ms so you can run one chunk during subsequent iterations of the event loop. You have one other solution which is to call the update_idletasks method of a widget periodically, but that’s more of a workaround than a fix.

Bear in mind that Tkinter is not thread safe, so using threads requires extra care. You can only call methods on widgets from the main thread, which means other threads must communicate with the main thread via a thread-safe queue.

Leave a Comment