python - 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(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/

相关文章:

java - 它是否使用 "super"在重写方法中调用 Overriden 方法,我们是否在调用中进行重载?

python - 返回字符串中单词的字典长度

C++ 类函数的回调

c++ - 编译器不理解递归函数来打印树级顺序 C++

java - 抽象方法

swift - 当函数具有不同签名时将函数作为参数传递

python - 从包导入时类属性会重置

python - Uwsgi 失败并返回 : Py_Initialize: Unable to get the locale encoding

python - Discord.py @bot.event

python - 如何删除相似的python类之间的重复