Le finestre root di Tkinter hanno un metodo chiamato after
che può essere utilizzato per pianificare una funzione da chiamare dopo un determinato periodo di tempo. Se la stessa funzione chiama after
, hai impostato un evento ricorrente automatico.
Ecco un esempio di lavoro:
# for python 3.x use 'tkinter' rather than 'Tkinter'
import Tkinter as tk
import time
class App():
def __init__(self):
self.root = tk.Tk()
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()
def update_clock(self):
now = time.strftime("%H:%M:%S")
self.label.configure(text=now)
self.root.after(1000, self.update_clock)
app=App()
Tenete a mente che after
non garantisce la funzione verrà eseguito esattamente in tempo. Solo orari il lavoro da eseguire dopo un determinato periodo di tempo. L'app è occupata potrebbe esserci un ritardo prima che venga chiamata poiché Tkinter è a thread singolo. Il ritardo viene in genere misurato in microsecondi.
fonte
2010-03-08 12:20:02
Ecco [codice di esempio su come usare 'root.after()' per implementare un timer.] (Https://gist.github.com/zed/5440b9372a15d86b5c47) – jfs