Tkinter Time Delay

You can add time delays in a Tkinter app.

Note: don’t use “import time” and “sleep()” for Tkinter, because you’ll have some weird issues.

Instead, use Tkinter’s after() function.

Example:

from Tkinter import *
root = Tk()

def message():
    L['text'] = 'I LIKE ICE CREAM'

def delay():
    L['text'] = 'Wait for it...'
    root.after(2000, message)

L = Label(root, text="Please click the button.")
L.pack()

B = Button(root, text="button", command=delay)
B.pack()

root.mainloop()

When the button, B, is clicked, the button calls the delay function.

This delay function immediately changes the label text of label L to “wait for it…” and then starts a time delay using the after() function:

root.after(2000, message)

The time is in milliseconds (2000 ms = 2 s). The second argument (“message”) is a callback function. In other words, this is the function that will be called when Python is done waiting the 2000 milliseconds.