重新映射tkinter中的默认键绑定 [英] remap default keybinding in tkinter

查看:94
本文介绍了重新映射tkinter中的默认键绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向我的根窗口(一个Tk()实例,其中包含一个文本小部件)添加了一个键绑定:

root.bind("Control-o", setconnpanelopen)

问题在于,control-o似乎默认情况下绑定到插入换行符",因此我的过程将被触发,但它还会插入一个换行符! 即使我将return 'break'放在其末尾,也会发生这种情况-因此它会在之前触发,因此看来.

1)我如何重新映射此行为?

2)我在任何地方都找不到默认的keybindings文本小部件列表-它们在某个地方可用?

解决方案

您想阅读"bindtags"-tkinter的绑定机制.

小部件具有一组按顺序处理的绑定标签"(或"bindtags").例如,文本窗口小部件具有四个标签:窗口小部件的标签,窗口小部件类的标签(这是内部类名称,而不是python类),顶级窗口的标签和"all"的标签.

大多数默认绑定都在类绑定上.这意味着您在窗口小部件上创建的所有特定绑定都发生在默认绑定之前.在这种情况下,由于绑定到根窗口,所以绑定发生在之后.

您可以随时创建一个绑定,以阻止事件沿绑定标签链向上传播.您可以通过从绑定中返回文字字符串"break"来实现.因此,如果绑定位于文本窗口小部件本身上,则可以通过使setconnpanelopen返回"break"来防止默认行为.由于您的原始绑定位于根窗口上,所以这无济于事,因为默认绑定发生在绑定之前.

您有两种解决方案:一种是将绑定放置在小部件上,而不是在根窗口上.或者,将其保留在根窗口上,以便无论哪个窗口小部件具有焦点都将触发它,然后将绑定添加到文本窗口小部件,该绑定除了返回"break"外什么都不做,以防止发生默认绑定.

有关绑定的最终列表,请参见 http://tcl .tk/man/tcl8.5/TkCmd/text.htm#M162 -指向tcl/tk,但这是tkinter的力量所在,它是tkinter文档的最终授权.

I am adding a keybinding to my root window (a Tk() instance, which contains a Text widget) :

root.bind("Control-o", setconnpanelopen)

The problem is that it seems that control-o is bound by default to "insert newline", so that my procedure fires, but it also inserts a newline! This happens even if I put a return 'break' at its end - so it fires before, so it seems.

1) how can I remap this behaviour?

2) I didnt find anywhere a list of the default keybindings of the text widgets - they are available somewhere?

解决方案

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.

这篇关于重新映射tkinter中的默认键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆