Skip to content Skip to sidebar Skip to footer

Python & Tkinter -> About Calling A Long Running Function That Freeze The Program

Am a new in GUI programming and I am trying to make a GUI for one of my python parser. I know that : Tkinter is single threaded. Screen updates happen on each trip through the e

Solution 1:

The problem is simple: BUT1 won't return until the call to main returns. As long as main (and thus, BUT1) doesn't return, your GUI will be frozen.

For this to work you must put main in a separate thread. It's not sufficient that main spawns other threads if all it's doing is waiting for those threads.

Solution 2:

If you call root.update() occasionally from the BUT1 function, that should prevent the GUI from freezing. You could also do that from a python thread with a fixed interval.

For example, updating every 0.1 seconds:

from threading import Thread
from time import sleep

self.updateGUIThread = Thread(target=self.updateGUI)

defupdateGUI(self):
    while self.updateNeeded
        root.update()
        sleep(0.1)

After the big function completes you can set self.updateNeeded to False.

Post a Comment for "Python & Tkinter -> About Calling A Long Running Function That Freeze The Program"