Generating an event in Tkinter

Fredrik Lundh fredrik at pythonware.com
Thu Sep 2 05:14:02 EDT 1999


<nharlow at my-deja.com> wrote:
> import time
> name="Analyze"
> while i< 100:
> print "%s: This is %s time through the loop"%(name,i+1)
> widget.event_generate("<<foo>>")
> widget.bind("<<foo>>", lambda e, y=1, x=time.sleep: x(y))
> i=i+1

> It just locks up; I don't even get any errors.

no errors at all?  not even some-
thing like:

Traceback (innermost last):
  File "example.py", line 3, in ?
    while i< 100:
NameError: i

???

if I set "i" to 0, and create a widget, 
your program works just fine.  or in
other words, the loop will now send
100 events to the widget, which,
whenever it gets around to check
the event queue, will block for
one second per event.

the following version increases the
chance that the widget actually gets
around to process your event:

from Tkinter import *
import time

widget = Tk()
widget.bind("<<foo>>", lambda e, y=1, x=time.sleep: x(y))

name="Analyze"

for i in range(100):
        print "%s: This is %s time through the loop"%(name,i+1)
        widget.event_generate("<<foo>>")
        widget.update() # process events

mainloop()

btw, I think you might be better off using a
timer [1], but that's me...

</F>

1) http://www.pythonware.com/library/tkinter/introduction/basic-widget-methods.htm
=> alarm handlers and other non-event callbacks





More information about the Python-list mailing list