disable tkinter keyboard shortcut (2)

后端 未结 2 458
借酒劲吻你
借酒劲吻你 2021-01-21 14:12

I\'m proposing a continuation of the discussion in disable tkinter keyboard shortcut: I have an event handler for an event that Tkinter also uses, so that my prog & Tkinter

2条回答
  •  -上瘾入骨i
    2021-01-21 15:04

    Bindings are handled in a specific order, defined by the bindtags of that widget. By default this order is:

    1. The specific widget
    2. The widget class
    3. The toplevel window
    4. The special class "all"

    If there are conflicting bindings -- for example, a control-b binding on both the widget and class -- they both will fire (in the described order) unless you break the chain by returning "break".

    In the case of the code you posted, however, you are binding to the toplevel window (ie: the root window), and the conflicting binding is on the class. Therefore, the binding will fire for the class before it is processed by the toplevel, so even if your binding returned "break" it wouldn't matter since the class binding happens first.

    The most straight-forward solution is to move your binding to the actual widget and return "break". That will guarantee your binding fires first, and the return "break" guarantees that the class binding does not fire.

    If you really want your binding on the root window, you can remove the binding for the class using the bind_class method with the value of "Text" for the class.

    You might find the Events and Bindings page on effbot.org to be useful.

提交回复
热议问题