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


任何应用程序中的选项卡顺序决定了该应用程序的哪个元素必须设置焦点。在 Tkinter 应用程序中,它会不断查找下一个需要聚焦的小部件。若要设置应用程序中的选项卡顺序,我们可以定义一个函数,选择所有小部件并使用 lift() 方法。该方法将允许该函数按编程方式在特定小部件上设置焦点。

示例

#Import the required libraries
from tkinter import *

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry of Tkinter Frame
win.geometry("700x350")

#Add entry widgets
e1 = Entry(win, width= 35, bg= '#ac12ac', fg= 'white')
e1.pack(pady=10)
e2 = Entry(win, width= 35)
e2.pack(pady=10)
e3 = Entry(win, width= 35, bg= '#aa23dd',fg= 'white')
e3.pack(pady=10)

#Change the tab order
def change_tab():
   widgets = [e3,e2,e1]
   for widget in widgets:
      widget.lift()

#Create a button to change the tab order
Button(win, text="Change Order", font=('Helvetica 11'), command= change_tab).pack()
win.mainloop()

输出

运行以上代码会显示一个窗口,其中包含一组 Entry 小部件和一个可更改每个小部件的选项卡顺序的按钮。

更新时间:2021 年 5 月 26 日

1K+ 浏览量

开启你的职业生涯

通过完成课程获取认证

开始学习
广告
© . All rights reserved.