[Tkinter-discuss] Re: Any way to extract from an event object the
eventspecifier string which fired the event? (Fredrik Lundh)
Kenneth McDonald
kenneth.m.mcdonald at sbcglobal.net
Tue May 11 15:36:57 EDT 2004
Fred,
Thanks for the feedback. I was already holding the event string
in Python and performing a mapping similar to what you've shown
below, but your method is quite a bit cleaner. I hadn't realized
(or even thought of the possibility) that the Tkinter event object
could be so easily expanded with further information, and that's
a big help.
It is a shame, though, that this can't actually be done from Tk
(so far as anyone has indicated.)
Cheers,
Ken
> Message: 1
> Date: Mon, 10 May 2004 22:49:54 +0200
> From: "Fredrik Lundh" <fredrik at pythonware.com>
> Subject: [Tkinter-discuss] Re: Any way to extract from an event object
> the eventspecifier string which fired the event?
> To: tkinter-discuss at python.org
> Message-ID: <c7optv$fg9$1 at sea.gmane.org>
>
> Kenneth McDonald wrote:
>
>> Let's say I have defined and bound a function to be executed
>> in response to an event:
>>
>> def callback(e):
>> ...
>>
>> When the function is called, e is an event object from which I
>> can extract various bits of info; for example, e.widget is the
>> widget on which the binding was invoked.
>>
>> However, I can't figure out any way to get the event string
>> specifying the firing event, i.e. the string by which 'callback'
>> was originally bound. From looking at Tk bind docs, this seems to
>> be a Tk lack rather than a Tkinter lack, but I'm wondering if
>> there is some other function in Tkinter/Tk which will let me
>> do this.
>
> here's one way to do it:
>
> def my_bind(widget, event, callback):
> def proxy(e, event=event, callback=callback):
> e.event_string = event
> return callback(e)
> widget.bind(event, proxy)
>
> def my_callback(e):
> print e.event_string
>
> my_bind(widget, "<Button-1>", my_callback)
> my_bind(widget, "<Key>", my_callback)
>
> </F>
>
>
>
>
>
>
> ------------------------------
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
> End of Tkinter-discuss Digest, Vol 3, Issue 4
> *********************************************
>
More information about the Tkinter-discuss
mailing list