Question about Tkinter

Fredrik Lundh fredrik at pythonware.com
Sun Aug 12 07:07:50 EDT 2001


Cameron Laird wrote:
> Confession time:  in principle, GUI coders also can code
> their own bindings and use %W substitution to compute
> the widget which received the event.  Does that work in
> Tkinter?

Tkinter passes an event instance to the event handler,
and %x codes are mapped to event instance attributes.

see table 7-2 for a list of portable attributes:
http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm

in this case, you want the event.widget attribute:

    w = SomeWidget()
    w.bind(..., callback)

    def callback(event):
        print event.widget

note that the widget attribute is a real Tkinter widget
object (not a Tk widget name), so you can do things
like:

    w1 = SomeWidget()
    w1.info = 1, somedata
    w1.bind(..., callback)

    w2 = SomeWidget()
    w2.info = 2, somedata
    w2.bind(..., callback)

    def callback(event):
        print event.widget.info
        print event.widget.cget("text")


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