[英]Is the Tkinter's Scale widget selectable in the Python by the keyboard with the 'Tab' key?
我使用“Tab”键在窗口中选择小部件。 我可以选择除 Scale 之外的所有需要操作的小部件(Entry、Checkbutton、Listbox、Buttons)。 可以这样选择吗?
我已经阅读了 Tkinter 文档和问题Python Tkinter:控制/设置(Tab 键)小部件“访问”顺序以及如何在 tkinter 应用程序中设置 Tab 顺序? 和其他一些人并没有找到答案。
这是工作示例:
from tkinter import *
optionsWindow = Tk() # = Toplevel(bd = 5) - for secondary window case
loadTableSign = 1
loadTableCheck = IntVar()
loadTableCheck.set(loadTableSign)
fnTable = 'table.txt'
optionsTableName = StringVar()
optionsTableName.set(fnTable)
saveOptionsSign = 0
saveOptionsCheck = IntVar()
saveOptionsCheck.set(saveOptionsSign)
textVideoModes=('3840 x 2160','3264 x 2448','2048 x 1536','1280 x 1024','1024 x 768','800 x 600','640 x 480')
videoModeNumber = 0
currentVideoMode = 'Off'
dx = 25
scaleVar = IntVar()
scaleVar.set(dx)
def escapeOptions(event): # Exit from window by <Esc> key (for this example only)
exit()
# The place for arrows keys event handlers (see below in the question) AAA
optionsWindow.title('Options')
optionsWindow.resizable(False,False)
optionsWindow.geometry('200x350+20+40')
#optionsWindow.focus_force() # not used for stand-alone window
loadTableCheck.set(loadTableSign)
optionsTableName.set(fnTable)
saveOptionsCheck.set(saveOptionsSign)
Label(optionsWindow,text = 'Table filename:').place(y = 5, x = 5)
Entry(optionsWindow,width = 12, text = optionsTableName).place(y = 5, x = 105)
Label(optionsWindow,text = 'Load table on start:').place(y = 25, x = 5)
Checkbutton(optionsWindow, variable = loadTableCheck, onvalue = 1, offvalue = 0).place(y = 24, x = 120)
Label(optionsWindow, text = 'Video Modes:').place(y=45, x=60)
lbVModes = Listbox(optionsWindow, height = 9)
for i in range(len(textVideoModes)):
lbVModes.insert(END, textVideoModes[i])
lbVModes.place(y = 65, x = 33)
#lbVModes.bind('<<ListboxSelect>>', videoModeSelect) # commented in this example
lbVModes.selection_set(videoModeNumber)
Label(optionsWindow, text = 'Current mode:').place(y = 205, x = 5)
Label(optionsWindow, text = currentVideoMode).place(y = 205, x = 90)
Scale(optionsWindow, label = 'Scroll value:', from_ = 25, to = 200, resolution = 25, tickinterval = 25, length = 175, sliderlength = 20, width = 7, orient = HORIZONTAL, variable = scaleVar).place(y = 225, x = 5)
Label(optionsWindow, text= 'Save options on OK:').place(y = 290, x = 5)
Checkbutton(optionsWindow, variable = saveOptionsCheck, onvalue = 1, offvalue = 0).place(y = 289, x = 120)
Button(optionsWindow, text = 'Ok', width = 6).place(y = 315, x = 45)
Button(optionsWindow, text = 'Cancel', width = 6).place(y = 315, x = 105)
optionsWindow.bind('<Escape>', escapeOptions)
# The place for binding of arrows keys (see below in the question) BBB
optionsWindow.mainloop()
结果:
我想可能需要Scale小部件的事件处理程序并制作它们(位置显示在上面的代码中,AAA 位置):
def keyRight(event):
global dx
if dx < 200:
dx += 25
scaleVar.set(dx)
def keyLeft(event):
global dx
if dx > 25:
dx -= 25
scaleVar.set(dx)
以及它们的绑定(代码中的 BBB 位置):
optionsWindow.bind('<Right>',keyRight)
optionsWindow.bind('<Left>',keyLeft)
处理程序工作正常(我可以随时使用 <- 和 -> 键更改显示的“滚动值”),但Scale小部件仍然无法选择。 这个问题有解决方案吗?
比例是可选择的 - 我可以运行您的代码并使用 Tab 键来选择小部件。 选择后,我可以使用箭头键调整值。 默认情况下,您无需执行任何操作即可获得该行为。
如果您要问如何有一个视觉指示它被选中,请将highlightthickness
属性设置为大于零的值。
解决另一个任务,也与Scale
小部件有关,我仔细阅读了Tkinterbook ,发现那里有配置选项takefocus
,这是这个问题的决定( takefocus=1
)。 还需要将highlightthickness
选项设置为非零值,以明确表示Bryan Oakley在他的回答中提到的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.