Python Tkinter内的定时回调?

Python Tkinter内的定时回调?,python,class,methods,callback,tkinter,Python,Class,Methods,Callback,Tkinter,我有一个TKinter GUI,它通过向外部程序写入数据并通过UDP连接从中读取数据来与外部程序交互。外部程序以1Hz的频率发送状态消息,我希望在TKinter类中有一个接收该数据的循环回调,但我不太确定这是如何实现的。以简化的形式,以下是我得到的: class App: def __init__(self,master): # Code that creates all the gui buttons, scales, etc def SendValues(s

我有一个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事件的情况下做到这一点


谢谢

在闲逛之后,我找到了自己的问题。定时回调可以使用.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)