我有一个 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)
关于python - Tkinter 中的定时回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16388630/