Tkinter内的定时回调?

2024-09-29 17:15:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个TKinter GUI,它通过向外部程序写入数据并通过UDP连接从中读取数据来与外部程序进行交互。外部程序以1Hz的频率发送一条状态消息,我希望在TKinter类中有一个接收该数据的循环回调,但我不太确定这是如何实现的。简而言之,我有:

class App:
    def __init__(self,master):
        # Code that creates all the gui buttons, scales, etc

    def SendValues(self,event):
        # Code that sends the values of all the scales upon a button press

    def ReceiveValues(self):
        # Code that receives UDP data and then sets Tkinter variables accordingly

我想让ReceiveValues方法每秒执行一次。我如何做到这一点而不中断所有其他可能发生的Tkinter事件?你知道吗

谢谢!你知道吗


Tags: the数据self程序thattkinterdefcode
1条回答
网友
1楼 · 发布于 2024-09-29 17:15:52

在四处打探之后,我发现了我自己的问题。可以使用.after()方法完成定时回调:

class App:
    def __init__(self):
        self.root = tk()

    def SendValues(self,event):
        # Code that sends the values of all the scales upon a button press

    def ReceiveValues(self):
        # Code that receives the values and sets the according Tkinter variables
        self.root.after(1000, self.ReceiveValues)

相关问题 更多 >

    热门问题