On Fri, 15 Jul 2005 10:20:48 +1200 (NZST)
[EMAIL PROTECTED] wrote:

> def clicked(w):
>     print 'Widget %s clicked! Text:' % (str(w), w.cget('text'))
> 
> def makeCallback(w):
>     def callback(e):
>         clicked(w)
>     return callback
> 
> # create labels
> for text in ['foo', 'bar', 'baz']:
>     lb = Label(master, text=text)
>     lb.bind('<Button-1>', makeCallback(lb))
> 

I don't think it will work this way, because you don't catch the event bind() 
passes to the callback
(you also use a variable "e" in makeCallback() that isn't defined anywhere).

You probably better use a lambda in this case:

def callback(text):
    print text

for text in ('foo', 'bar', 'baz'):
    lb = Label(master, text=text)
    lb.pack()
    lb.bind('<1>', lambda event, t=text: callback(t))

or use event.widget to determine which label was clicked:

def callback(event):
    print event.widget['text']

for text in ('foo', 'bar', 'baz'):
    lb = Label(master, text=text)
    lb.pack()
    lb.bind('<1>', callback)

I hope this helps

Michael
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to