Multi threading in Tkinter GUI, threads in different classes

前端 未结 2 1719
一整个雨季
一整个雨季 2020-12-20 18:47

I\'m currently learning the Tkinter GUI programming. And I\'m stuck in somewhere in multi threading concept. Even though this topic is discussed several times here, I couldn

相关标签:
2条回答
  • 2020-12-20 19:35

    Pythonista's answer is excellent. But I'd like to touch upon some additional points.

    • GUI's are event-driven. They run in a loop processing events, calling pieces of your code (called callbacks) every now and then. So your code is more or less a guest in the event-loop. As you have noticed, your pieces of code should finish quickly otherwise they stall event processing making the GUI unresponsive. This is a completely different programming model from the linear programs that is often seen in tutorials. To perform longer-running calculations or tasks you can split them up in small pieces and use after. Or you could to them in another process with multiprocessing. But then you'd still need to check periodically (with after again) if they have finished.

    The following points stem from the fact that doing multithreading right is hard.

    • CPython (the most used Python implementation) has what is called a Global Interpreter Lock. This ensures that only one thread at a time can be executing Python bytecode. When other threads are busy executing Python bytecode, the thread running the GUI is doing nothing. So multithreading is not a certain solution to the problem of an unresponsive GUI.

    • a lot of GUI toolkits are not thread-safe, and tkinter is not an exception. This means that you should only make tkinter calls from the thread that's running the mainloop. (In Python 3.x, tkinter has been made thread safe.)

    0 讨论(0)
  • 2020-12-20 19:48

    You don't need threading for something this simple.

    The GUI is freezing because you're putting a time.sleep inside the function which is blocking the main thread until it's finished.

    Simply use Tk's built in after method. Change your function to.

    def countNum(self, num=0):
        if num < 10:
            print num
            root.after(2000, lambda: self.countNum(num + 1))
        else:
            print "Stopping after call"
    

    The after method takes the following arguments:

    after(delay_ms, callback, arguments)
    

    The time is in milliseconds, and 1000 ms = 1 second. So, we pass 2,000 ms for a 2 second delay.

    0 讨论(0)
提交回复
热议问题