How to run a function in the background of tkinter

后端 未结 5 2166
独厮守ぢ
独厮守ぢ 2021-01-04 21:48

I am new to GUI programming and I want to write a Python program with tkinter. All I want it to do is run a simple function in the background that can be influenced through

5条回答
  •  一个人的身影
    2021-01-04 21:54

    I don't have sufficient reputation to comment on Bryan Oakley's answer (which I found to be very effective in my program), so I'll add my experience here. I've found that depending on how long your background function takes to run, and how precise you want the time interval to be, it can be better to put self.after call at the beginning of the recurring function. In Bryan's example, that would look like

    def add_one(self):
        self.after(1000, self.add_one)
        self.counter += 1
    

    Doing it this way ensures that the interval of time is respected exactly, negating any interval drift that might occur if your function takes a long time.

提交回复
热议问题