[Tutor] Tk -- which label clicked

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Fri Jul 15 00:20:48 CEST 2005


Quoting Ron Weidner <xecronix at yahoo.com>:

> Or, more to the point... I need to dynamicaly create
> clickable labels, each using the same callback. When
> clicked, I need to know which one was clicked. I was
> hoping to use the "text" of the label as the argument
> to another function.

You've got one suggestion ... But, FWIW, here is how I would probably do it:

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))

--------------

(this involves a separate callback function for each label, so it could possibly
be slower)

-- 
John.


More information about the Tutor mailing list