Tkinter中的定时回调?

3

我有一个使用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事件的情况下实现这一点?
谢谢!

https://dev59.com/AXE95IYBdhLWcg3wPbZ7 - kalgasnik
1个回答

2

在做了一些探索后,我解决了自己的问题。使用.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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接