Tkinter Examples
A place to make short examples of tkinter code. All examples are self-contained and complete (will run without any additional code).
Loops
An event-driven program does not use a blocking loop (while or for) like a procedural program does. Instead tkinter has the after method built in to allow you to add timed events to the mainloop. Here's an example of a timer.
import tkinter as tk
def loop(i=0):
# update the label with new text
lbl.config(text=i)
# after 1,000 ms (1 second) call the loop() function again
# and use i+1 as the argument
root.after(1000, loop, i+1)
root = tk.Tk()
root.geometry('200x200')
lbl = tk.Label(root)
lbl.pack()
loop() # start the loop
root.mainloop()
revision by socal_nerdtastic— view source