Timer in Tkinter?

Matthew Dixon Cowles matt at mondoinfo.com
Fri Aug 17 04:32:03 CEST 2001


On Thu, 16 Aug 2001 22:31:04 -0000, moonlite56 at yahoo.com
<moonlite56 at yahoo.com> wrote:

>I am also creating a little vitual pet that updates its mood and
>image every second or so. Basicly, it uses a timer. I used them a lot
>in Visual Basic, and I wondered if there was such a thing in
>Tkinter. I don't know how time.sleep() would work, and it seems I
>need an event. Is there a way to do this?

Yes, there is. Use the after method:

>>> from Tkinter import *
>>> def printSomething():
...   print "wibble"
... 
>>> root=Tk()
>>> root.after(2000,printSomething)
'after#2'
>>> wibble

The first argument to after is the number of milliseconds to wait and
the second argument is the function to call. For information like this
about Tkinter, I recommend Fredrik Lundh's excellent An Introduction
to Tkinter. It's at:

http://www.pythonware.com/library/tkinter/introduction/index.htm

I almost always have a local copy open when I'm doing Tkinter
programming.

Regards,
Matt



More information about the Python-list mailing list