如何在 tkinter 应用程序中设置选项卡顺序?

How to set the tab order in a tkinter application?(如何在 tkinter 应用程序中设置选项卡顺序?)

本文介绍了如何在 tkinter 应用程序中设置选项卡顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种在 tkinter 应用程序中设置标签顺序的方法,我一直在研究.目前默认顺序似乎是自上而下的,但它需要使用 CTRL + Tab 来循环浏览控件.

I have been searching for a way to set the tab order in a tkinter application, that I have been working on. Currently the default order seems to be working from top-down, but it requires using CTRL + Tab to cycle through the controls.

有没有办法自定义顺序,更重要的是,将 CTRL + Tab 改为 Tab?

Is there any way to customize the order and, more so, change the CTRL + Tab to just Tab?

推荐答案

Tab 顺序基于堆叠顺序,而堆叠顺序又默认为创建小部件的顺序.您可以使用方法tkraise(或lift)和lower 来调整堆叠顺序(因此也可以调整tab 顺序).

Tab order is based on the stacking order, which in turn defaults to the order that widgets are created. You can adjust the stacking order (and thus the tab order) using the methods tkraise (or lift) and lower.

这对您来说应该是开箱即用的,无需按 CTRL + Tab.但是请注意,该选项卡会在文本小部件中插入文字选项卡,而不是将焦点移动到另一个控件.当然可以更改默认行为.

This should be working out of the box for you without the need to press CTRL + Tab. Be aware, however, that tab inserts a literal tab in text widgets rather than moving focus to another control. That default behavior can be changed of course.

下面的示例展示了如何反转 Tab 键顺序.运行示例时,在第一个条目中按 Tab 应该会将您带到最后一个条目.再次按 Tab 键会将您带到第二个,然后是第一个,起泡,冲洗,重复

Here's an example showing how to reverse the tab order. When running the example, pressing tab in the first entry should take you to the last. Pressing tab again takes you to the second, then the first, lather, rinse, repeat

请注意,原生 Tk 命令是 raiselower,但由于 raise 是Python 它必须在 tkinter 中重命名.

Note that the native Tk commands are raise and lower, but since raise is a reserved word in Python it had to be renamed in tkinter.

import Tkinter as tk


class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        e1 = tk.Entry(self)
        e2 = tk.Entry(self)
        e3 = tk.Entry(self)

        e1.insert(0,"1")
        e2.insert(0,"2")
        e3.insert(0,"3")

        e1.pack()
        e2.pack()
        e3.pack()

        # reverse the stacking order to show how
        # it affects tab order
        new_order = (e3, e2, e1)
        for widget in new_order:
            widget.lift()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

既然您提到您必须执行 CTRL + Tab,我猜您正试图让 Tab 键从文本小部件更改焦点.通常一个 tab 键插入一个文字 tab.如果你想让它改变焦点,只需添加一个绑定到 <Tab> 事件.

Since you mention you have to do CTRL + Tab, I'm guessing you're trying to have the tab key change focus from a text widget. Normally a tab key inserts a literal tab. If you want it to change the focus, just add a binding to the <Tab> event.

Tkinter 有一个函数可以返回下一个应该获得焦点的小部件的名称.不幸的是,对于旧版本的 Tkinter,该功能是错误的.但是,很容易解决这个问题.您可以在上面的代码中添加以下几种方法:

Tkinter has a function that will return the name of the next widget that should get focus. Unfortunately, for older versions of Tkinter that function is buggy. However, it's easy to work around that. Here's a couple of methods you can add to the above code:

    def _focusNext(self, widget):
        '''Return the next widget in tab order'''
        widget = self.tk.call('tk_focusNext', widget._w)
        if not widget: return None
        return self.nametowidget(widget.string)

    def OnTextTab(self, event):
        '''Move focus to next widget'''
        widget = event.widget
        next = self._focusNext(widget)
        next.focus()
        return "break"

这篇关于如何在 tkinter 应用程序中设置选项卡顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 tkinter 应用程序中设置选项卡顺序?

基础教程推荐