tkinter: how to use after method

Question:

Hey I am new to python and am using tkinter for my gui. I am having trouble using the “after” method.
The goal is to make a random letter appear every 5 seconds.

Here is my code:

import random
import time
from tkinter import *


root = Tk()

w = Label(root, text="GAME")
w.pack()

frame = Frame(root, width=300, height=300)
frame.pack()

L1 = Label(root, text="User Name")
L1.pack(side=LEFT)
E1 = Entry(root, bd =5)
E1.pack(side=LEFT)


tiles_letter = ['a', 'b', 'c', 'd', 'e']


while len(tiles_letter) > 0:
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    frame.after(500)
    tiles_letter.remove(rand)  # remove that tile from list of tiles

root.mainloop()

can someone please help me — the problem is definitely frame.after(500):
i’m not sure if it is correct to use “frame” and I don’t know what which argument follows the 500.

Thanks

Asked By: user2456977

||

Answers:

You need to give a function to be called after the time delay as the second argument to after:

after(delay_ms, callback=None, *args)

Registers an alarm callback that is called after a given time.

So what you really want to do is this:

tiles_letter = ['a', 'b', 'c', 'd', 'e']

def add_letter():
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    root.after(500, add_letter)
    tiles_letter.remove(rand)  # remove that tile from list of tiles


root.after(0, add_letter)  # add_letter will run as soon as the mainloop starts.
root.mainloop()

You also need to schedule the function to be called again by repeating the call to after inside the callback function, since after only executes the given function once. This is also noted in the documentation:

The callback is only called once for each call to this method. To keep
calling the callback, you need to reregister the callback inside
itself

Note that your example will throw an exception as soon as you’ve exhausted all the entries in tiles_letter, so you need to change your logic to handle that case whichever way you want. The simplest thing would be to add a check at the beginning of add_letter to make sure the list isn’t empty, and just return if it is:

def add_letter():
    if not tiles_letter:
        return
    rand = random.choice(tiles_letter)
    tile_frame = Label(frame, text=rand)
    tile_frame.pack()
    root.after(500, add_letter)
    tiles_letter.remove(rand)  # remove that tile from list of tiles

Live-Demo: repl.it

Answered By: dano

I believe, the 500ms run in the background, while the rest of the code continues to execute and empties the list.

Then after 500ms nothing happens, as no function-call is implemented in the after-callup (same as frame.after(500, function=None))

Answered By: Timbo

after is used to delay execution of the program or to execute a command in background sometime in the future. But you can build a loop inside the mainloop by calling itself.

import tkinter as tk #import tkinter
import datetime #import datetime for our clock

def tick(): #function to update the clock
    showed_time = clock['text'] #current showed time
    current_time = datetime.datetime.now().strftime("%H:%M:%S") #real time
    if showed_time != current_time: #if the showed time is not the real time
        clock.configure(text=current_time) #update the label with the current time
    clock.after(1000, tick) #call yourself in 1000ms (1sec.) again to update the clock
    return None

root=tk.Tk()

clock = tk.Label(root)
clock.pack()
tick()

root.mainloop()

In the above script we had built a digital clock and get in touch with the after method. The after method is nothing but an interval and on the end of that interval we want that something happen.

To learn more about this basic widget method [click]

after(delay_ms, callback=None, args)

This method registers a callback function that will be called after a
given number of milliseconds
. Tkinter only guarantees that the
callback will not be called earlier than that
; if the system is busy,
the actual delay may be much longer.

import tkinter as tk 
import datetime 

def tick():
    showed_time = clock['text']
    current_time = datetime.datetime.now().strftime("%H:%M:%S")
    if showed_time != current_time:
        clock.configure(text=current_time)
    global alarm #make sure the alarm is reachable
    alarm = clock.after(1000, tick)#assign the alarm to a variable
    return None
def stop():
    stop.after_cancel(alarm) #cancel alarm
    

root=tk.Tk()

clock = tk.Label(root)
clock.pack()
stop = tk.Button(root, text='Stop it!', command=stop)
stop.pack()
tick()


root.mainloop()

Here we have the same code but with the ability to cancel our loop with the after_cancel method of tkinter. You dont need to global the alarm inside a class. self.alarm = self.clock.after(...) works fine.

after_cancel(id)

Cancels an alarm callback.

id

Alarm identifier.

Answered By: Thingamabobs