Basic query regarding bindtags in tkinter

前端 未结 1 863
轻奢々
轻奢々 2020-11-22 11:08

In the given example from this post, it was mentioned that if default bindtags are used then event value will not be visible inside definition (there will be lag by one).

相关标签:
1条回答
  • 2020-11-22 11:29

    When you do a binding on a widget, you aren't actually binding to a widget per se. When you do mywidget.bind(...), what is actually happening is that the binding is associated with a bind tag with the same name as the widget.

    When an event is detected, Tkinter first figures out which widget intercepted the event. This widget will have a list of zero or more (by default: four) bind tags associated with it. Tkinter will check each tag in order to see if there's a binding that matches the event. If it finds one, it will execute the binding and then continue to the next tag, until it runs out of tags or one of the bound functions returns the string "break".

    The sequence looks something like this:

    1. You press the "x" key. At this point the "x" hasn't been inserted anywhere
    2. Tkinter gets the bind tags for the widget that has the keyboard focus.
    3. By default the first bind tag is the widget itself. Is there a binding on that tag? If so, execute it. For example, you might print the contents of the widget. Because no other bindings have fired yet, the "x" will not be part of the contents.
    4. If the bound function returns "break" then no more event processing is done. The "x" will not get inserted into the widget.
    5. If the widget did not return "break", tkinter proceeds to the next bind tag.
    6. By default, the next bind tag is the widget class. Is there a binding on the class that matches this event (a keypress of the letter "x")?
    7. In this case the class does have a binding for this event, which is to actually insert the "x" into the widget. Until this point, the "x" still hasn't been inserted into the widget. After this binding, however, if you did a print of the contents you would see the "x"
    8. Processing continues in this manner until all bind tags have been processed.

    Based on the ongoing discussion in the comment section, it appears this is still unclear. I'll try to make this as simple as possible:

    It is the class binding which copies a character from the in-memory event object to the widget and thus causing it to appear on screen. Before the class binding fires, the character will not appear in the widget. After the class binding it will be in the widget.

    0 讨论(0)
提交回复
热议问题