tkinter event object question
Fredrik Lundh
fredrik at pythonware.com
Fri Jul 6 03:07:26 EDT 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:
http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm
> 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:
http://www.pythonware.com/library/tkinter/introduction/button.htm
> b2= Button(root,text='b2')
> b2.bind("<Button-1>",callme)
> b2.pack()
>
> root.mainloop()
> ------------------------------
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list