tkinter event object question

Fredrik Lundh fredrik at
Fri Jul 6 09:07:26 CEST 2001

Arun Ramanathan wrote:
> Is there a way by which I can determine which tkinter widget
> actually generated the event.
> Example if I had two buttons b1 and b2 initialized as shown below
> and bound to the event handler callme.
> -------------------------------
> from Tkinter import *
> root = Tk()
> def callme(event)
>     print "santa is coming"

    print event.widget

also see table 7-2 on this page:

> b1 = Button(root,text='b1')
> b1.bind("<Button-1>",callme)
> b1.pack()

you shouldn't do this, of course: the right way to handle
button clicks is to use the "command" option.

the command option takes a single callable, so you have
to use a lambda (or a local function) to share callbacks:

    def callme(widget):
        print widget

    b1 = Button(root, text='b1',
        command=lambda b1=b1: callme(b1)

(the b1=b1 thing captures the current value of b1, so we can
pass it to the callback when the lambda is executed)

also see:

> b2= Button(root,text='b2')
> b2.bind("<Button-1>",callme)
> b2.pack()
> root.mainloop()
> ------------------------------


<!-- (the eff-bot guide to) the python standard library:

More information about the Python-list mailing list