[Tkinter-discuss] Re: Any way to extract from an event object the eventspecifier string which fired the event?

Fredrik Lundh fredrik at pythonware.com
Mon May 10 16:49:54 EDT 2004


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>






More information about the Tkinter-discuss mailing list