In a tkinter application I need to plan some actions, to be executed after pressing a button. But after pressing the interface does not update.
from tkinter import Tk, Label, Button
from sched import scheduler
def label_update(label: Label, new_text: str):
label.confgure(text=new_text)
root = Tk()
label = Label(root, text="0")
label.pack()
s = scheduler()
s.enter(3, 1, label_update, (label, "1"))
s.enter(6, 1, label_update, (label, "2"))
Button(root, command=lambda: s.run(blocking=False), text="Start").pack()
root.mainloop()
The expected result is that the text on the label should automatically become 1 and after 2.
I don’t use .after() because label_update() in my application is much more complex and also manages a timer must be very precise. It would also require many more if cycles.
Here an example of how schedule action:
self.s = scheduler()
for time in range(self._TOTAL_TIME - 30, self._TOTAL_TIME):
self.s.enter(time, 2, self.update_main)
for time in range(1, self._TOTAL_TIME - 30):
self.s.enter(time, 1, self.update_entry)
self.s.enter(time, 2, self.update_main)
Maybe an asynchronous process could be useful, but I don’t know how to set it up in a situation like this.