nechto
nechto

Reputation: 23

I have no idea how the after method for tkinter works

I'm trying to make an intercom in python just for fun.

I want it to write "Err" and blink for 5 seconds when you try to call a number more than 69. But time.sleep() won't work in tkinter and I don't know how to work with the .after() method.

    if int(lbl.cget("text"))>69:
    for i in range(5):
        lbl.configure(text="Err")
        time.sleep(0.5)
        lbl.configure(text="     ")
        time.sleep(0.5)

Upvotes: 2

Views: 54

Answers (1)

acw1668
acw1668

Reputation: 46669

You can use .after() to execute a function every half second to toggle the text of the label:

def do_blink(lbl, n=10):
    if n > 0:
        lbl.after(500, do_blink, lbl, n-1) # call again after half a second
        lbl["text"] = "" if n&1 else "Err" # toggle between "" and "Err"

if int(lbl["text"]) > 69:
    do_blink(lbl)

Upvotes: 3

Related Questions