[Tutor] Tk -- which label clicked
Michael Lange
klappnase at freenet.de
Fri Jul 15 10:42:33 CEST 2005
On Fri, 15 Jul 2005 10:20:48 +1200 (NZST)
jfouhy at paradise.net.nz 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
More information about the Tutor
mailing list