【问题标题】:Timed Callback within Tkinter?Tkinter 内的定时回调?
【发布时间】:2013-05-05 20:07:46
【问题描述】:

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

谢谢!

【问题讨论】:

标签: python class methods callback tkinter


【解决方案1】:

在四处摸索后想出了我自己的问题。可以使用 .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)

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多