python 将焦点从一个 Text 小部件更改为另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1450180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the focus from one Text widget to another
提问by LJM
I'm new to Python and I'm trying to create a simple GUI using Tkinter.
我是 Python 新手,我正在尝试使用 Tkinter 创建一个简单的 GUI。
So often in many user interfaces, hitting the tab button will change the focus from one Text widget to another. Whenever I'm in a Text widget, tab only indents the text cursor.
通常在许多用户界面中,点击选项卡按钮会将焦点从一个文本小部件更改为另一个。每当我在文本小部件中时,选项卡只会缩进文本光标。
Does anyone know if this is configurable?
有谁知道这是否可以配置?
回答by Bryan Oakley
This is very easy to do with Tkinter.
使用 Tkinter 很容易做到这一点。
There are a couple of things that have to happen to make this work. First, you need to make sure that the standard behavior doesn'thappen. That is, you don't want tab to both insert a tab and move focus to the next widget. By default events are processed by a specific widget prior to where the standard behavior occurs (typically in class bindings). Tk has a simple built-in mechanism to stop events from further processing.
有几件事必须发生才能使这项工作发挥作用。首先,您需要确保标准行为不会发生。也就是说,您不希望 tab 既插入一个 tab 又将焦点移到下一个小部件。默认情况下,事件在标准行为发生之前由特定小部件处理(通常在类绑定中)。Tk 有一个简单的内置机制来阻止事件进一步处理。
Second, you need to make sure you send focus to the appropriate widget. There is built-in support for determining what the next widget is.
其次,您需要确保将焦点发送到适当的小部件。内置支持确定下一个小部件是什么。
For example:
例如:
def focus_next_window(event):
event.widget.tk_focusNext().focus()
return("break")
text_widget=Text(...)
text_widget.bind("<Tab>", focus_next_window)
Important points about this code:
关于此代码的要点:
- The method
tk_focusNext()
returns the next widget in the keyboard traversal hierarchy. - the method
focus()
sets the focus to that widget - returning
"break"
is critical in that it prevents the class binding from firing. It is this class binding that inserts the tab character, which you don't want.
- 该方法
tk_focusNext()
返回键盘遍历层次结构中的下一个小部件。 - 该方法
focus()
将焦点设置为该小部件 - 返回
"break"
很关键,因为它可以防止类绑定被触发。正是此类绑定插入了您不想要的制表符。
If you want this behavior for all text widgets in an application you can use the bind_class()
method instead of bind()
to make this binding affect all text widgets.
如果您希望应用程序中的所有文本小部件具有此行为,您可以使用该bind_class()
方法而不是bind()
使此绑定影响所有文本小部件。
You can also have the binding send focus to a very specific widget but I recommend sticking with the default traversal order, then make sure the traversal order is correct.
您还可以让绑定将焦点发送到一个非常特定的小部件,但我建议坚持使用默认的遍历顺序,然后确保遍历顺序是正确的。
回答by Eli
It is really simple in PyQt4 simply use this one single line below and you will be able to change focus by pressing tab button:
在 PyQt4 中这真的很简单,只需使用下面的这一行,您就可以通过按 Tab 按钮来更改焦点:
self.textEdit.setTabChangesFocus(True)
self.textEdit.setTabChangesFocus(True)
回答by dlamblin
The focus traversal is somewhat customizable, usually letting the X windows manager handle it (with focus follows mouse, or click). According to the manualit should be possible to bind
an event to the key press event, for tab presses, and triggering a focusNext
event in those cases.
焦点遍历在某种程度上是可定制的,通常让 X 窗口管理器处理它(焦点跟随鼠标或单击)。根据手册,应该可以bind
对按键事件产生事件,对于标签按下,并focusNext
在这些情况下触发事件。
回答by Charles Ritchie
Not sure about TKinter, but with PyQt, one can connect a function to the tab changed signal emitted by the tab box(the signal also carries an int value). But as far as I am aware Qt is pretty good at doing the right thing without it being specified.
不确定 TKinter,但使用 PyQt,可以将函数连接到选项卡框发出的选项卡更改信号(该信号还带有一个 int 值)。但据我所知,Qt 非常擅长在没有指定的情况下做正确的事情。