[Tutor] Python Tkinter event activated with time

Peter Otten __peter__ at web.de
Wed Jul 27 09:46:55 CEST 2011


Emeka wrote:

> I am putting together a small game, and I would want to enable my callback
> function using time passed.
> 
> How to do something like this with Tkinter event.
> 
> from time import time
> 
> ftime  = time()
>    if ftime - time() > 2000:
>         dosomething

You can schedule a function call with the after() method:

import Tkinter as tk

def scroll(s):
    while True:
        yield s
        s = s[-1] + s[:-1]

hello = scroll("Hello world! *** ")

def do_something():
    label["text"] = next(hello)
    # play it again, after 100 msecs
    label.after(100, do_something) 

root = tk.Tk()
label = tk.Label(root, font=("Times", 36))
label.pack()
do_something()
root.mainloop()




More information about the Tutor mailing list