Python Tkinter after()

This article covers the after method in Python Tkinter

The Tkinter after method is used to trigger a function after a certain amount of time. As you can imagine, this is an exceptionally useful function that can be used in a variety of different ways.

If you’ve been using the sleep() function in your GUI programs, you’ve been doing it wrong. We’ll have a small section on a comparison between the two.


The After Method – Syntax

The after method takes two parameters, the time delay and a function name. The function that has been passed will be called after the given time delay has passed.

It’s not a very practical example in the code shown below, but it gets the point across. Copy the code over into your own python file and run it to see the magic.

Just so you know, the after() function takes the time as milliseconds. 5000 milliseconds evaluate to 5 seconds.

from tkinter import *

def destroy_timer():
    root.destroy()

root = Tk()
root.geometry('300x300')

root.after(5000, destroy_timer)

root.mainloop()

Remember not to include the parenthesis when writing the function name. This is because we’re simply storing the function name for us later. Passing destroy_timer() would call the function as soon as the after() method is executed.


Examples

There isn’t much else to explain about this function so we’ll simply show some more uses of it, alongside other Tkinter widgets and functions.

The below code is a much more practical usage of the after method. After 3 seconds, it changes the color and text of the Label widget using configure.

from tkinter import *

def update():
    mylabel.configure(fg = "blue", text = "This is some blue text")

root = Tk()
root.geometry('200x200')

mylabel = Label(root, text = "This is some black text")
mylabel.pack(pady = 5)

root.after(3000, update)

root.mainloop()

Below is a short video demonstrating the effect of the above code.


after() vs sleep()

Most python programmers will know about the sleep() function from the time class. Many of you will have already used it. In this section we’ll explain why after() is the better choice.

The sleep() function works by putting the entire thread the program is executing on to “sleep”. This results in your entire program freezing for the duration of the “sleep”. Imagine playing a game and trying to implement a time delayed action with sleep(). The entire game would freeze until the action was carried out. Not pretty.

We’ll demonstrate this with two different code blocks, the first using the after() method and the second using time.sleep().

from tkinter import *

def timer():
    print("This is the after() method's output")

root = Tk()
root.geometry('300x300')

print("This will print before the after() method")
root.after(3000, timer)
print("This will print after the after() method")

root.mainloop()

This is what the output on the IDLE looks like. Notice that the output of the after() method arrived last, despite being before the second print statement.

This will print before the after() method
This will print after the after() method
This is the after() method's output

Now the sleep() function.

from tkinter import *
import time

def timer():
    time.sleep(5)
    print("This is the sleep function's output")

root = Tk()
root.geometry('300x300')

print("This will print before the after() method")
timer()
print("This will print after the after() method")

root.mainloop()

See the difference? It’s because unlike the after() method the sleep() function will pause the entire thread till it’s finished.

This will print before the after() method
This is the sleep function's output
This will print after the after() method

You might want to consider the use of Python lambdas with the after() function. You could spare yourself the trouble of creating an entire function, and simply write one line using a lambda instead.


Video Code

The Code from our Video on the Tkinter After Method on our YouTube Channel for CodersLegacy.

import tkinter as tk
import sys

def printsomething():
    print("This is the text in the function")
    
root = tk.Tk()
root.geometry('300x300')

print("Before the after call")
root.after(2000, printsomething)
print("after the after call")

root.mainloop()

This marks the end of the Python Tkinter after method article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions regarding the article material can be asked in the comments section below.

Related Articles:

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments