Tkinter .after method freezing window?

前端 未结 2 1022
日久生厌
日久生厌 2021-01-03 02:03

I have a simple chat client that I was attempting to get working with Tkinter as the interface. My problem is that when running the mainloop with

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-03 02:49

    after executes the callback function after the given time; however, this method also runs in the main thread. So if there is an operation that takes more time than usual (in this case, the blocking recvfrom), the GUI will be unresponsive until the complete callback is executed.

    To solve this, a common recipe is to spawn a new thread and communicate it with your Tkinter code with a synchronized object like a Queue. Thus, you put the data in the queue when you receive it from the socket, and then check periodically in the main thread inside the after callback.

    This is a question whose answer can be adapted to use the same approach: Tkinter: How to use threads to preventing main event loop from "freezing"

提交回复
热议问题