2

I have a TKinter GUI that interacts with an outside program by writing data to it and reading data from it over a UDP connection. The external program sends a status message at 1Hz, and I would like to have a looping callback within my TKinter class that receives that data, but I'm not quite sure how this is done. In a simplified form, here's what I have:

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

I want to have the ReceiveValues method execute once per second. How do I do this without interrupting all the other Tkinter events that could be happening?

Thanks!

4

1 回答 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)
于 2013-05-05T20:35:30.813 回答