我应该如何在tkinter中创建一个不会被聚焦的小部件?
例如,当我按下TAB键时,跳过一个按钮并不聚焦于它。
import tkinter
root = tkinter.Tk()
but1 = tkinter.Button(root, text ="Button 1")
but1.pack()
butNoFocus = tkinter.Button(root, text ="Button no focus", takefocus = 0)
butNoFocus.pack()
but2 = tkinter.Button(root, text = "Button 2")
but2.pack()
root.mainloop()
< p >将 takefocus
选项设置为 0
将禁用在 butNoFocus
上的标签聚焦。
我知道这是一个老问题,但对于任何未来的读者,一种更简单的方法来删除小部件的循环焦点是通过取消绑定<<NextWindow>>
,正如Bryan Oakley在this post中所述。
import tkinter as tk
root = tk.Tk()
button1 = tk.Button(root, text='Hello') # Two example buttons
button2 = tk.Button(root, text='World!')
button1.pack(ipadx=15, ipady=10)
button2.pack(ipadx=10, ipady=10)
root.unbind_all('<<NextWindow>>') # Unbinding the behavior that causes Tab Cycling
root.mainloop()
-takefocus
设置为0会更容易。