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
Set your binding on the text widget, not on the root. (Whole toplevel bindings are processed after widget class bindings – where the standard <Control-Key-b>
binding is – and those are processed after the widget instance bindings, which is what you want to use here.) And you need to do that 'break'
; it inhibits the subsequent bindings. (If you're having any problems after that, it's probably that the focus is wrong by default, but that's easy to fix.)
The only other alternative is to reconfigure the bindtags so that class bindings are processed after toplevel bindings, but the consequences of doing that are very subtle and far-reaching; you should use the simpler approach from my first paragraph instead as that's the normal way of handling these things.
Bindings are handled in a specific order, defined by the bindtags
of that widget. By default this order is:
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.