remap default keybinding in tkinter remap default keybinding in tkinter tkinter tkinter

remap default keybinding in tkinter


You want to read up on "bindtags" -- tkinter's binding mechanism.

Widgets have a set of bind "tags" (or "bindtags") that are processed in order. For example, a text widget has four tags: the tag for the widget, the tag for the widget class (which is an internal class name, not the python class), a tag for the toplevel window, and a tag for "all".

Most default bindings are on the class bindings. That means that any specific bindings you create on a widget happen before the default bindings. In this particular case, since you are binding to the root window, your binding is happening after.

At any time, you can create a binding that stops the propagation of the event up the bindtag chain. You do this by returning the literal string "break" from your binding. Thus, if your binding was on the text widget itself you could prevent the default behavior by having setconnpanelopen return "break". Since your original binding is on the root window this won't help, since the default binding happens before your binding.

You have a couple of solutions: one, place the binding on the widget rather than on the root window. Or, leave it on the root window so it will fire no matter which widget has focus, and then add a binding to the text widget that does nothing but return "break" to prevent the default binding from occurring.

For a definitive list of the bindings, see http://tcl.tk/man/tcl8.5/TkCmd/text.htm#M162 -- this points to tcl/tk, but that is what powers tkinter and it is the final authority when it comes to tkinter documentation.


Create your own widget that inherits from the text widget. You may want to put your callback function as a method of your widget as well.

class MyTextWidget(Text):    def __init__(self, master, **kw)        Text.__init__(self, master, **kw)        self.bind('<Control-O>', setconnpanelopen)

Make sure that your setconnpanelopen function returns 'break'.

You may want to have a look at this: http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm