Program freezing during the execution of a function in Tkinter

前端 未结 1 808
暗喜
暗喜 2020-11-28 12:52

I\'ve created a little GUI for one of my scripts. All is working well.

When I click on one Button, it launches a big function that is parsing a lot of data from som

相关标签:
1条回答
  • 2020-11-28 13:30

    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.

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