Run an infinite loop in the backgroung in Tkinter

后端 未结 2 2019
慢半拍i
慢半拍i 2020-12-06 14:56

I would like the code to run in the background and to update my GUI periodically. How can I accomplish this?

For example, suppose I want to execute something like t

2条回答
  •  时光说笑
    2020-12-06 15:32

    If you truly want to run a distinct infinite loop you have no choice but to use a separate thread, and communicate via a thread safe queue. However, except under fairly unusual circumstances you should never need to run an infinite loop. Afte all, you already have an infinite loop running: the event loop. So, when you say you want an infinite loop you are really asking how to do an infinite loop inside an infinite loop.

    @mgilson has given a good example on how to do that using after, which you should consider trying before trying to use threads. Threading makes what you want possible, but it also makes your code considerably more complex.

提交回复
热议问题