我用'Tab'键在窗口中选择小部件。我可以选择需要缩放以外的所有小部件(输入,复选框,列表框,按钮)。完全可以通过这种方式选择吗?
我已经阅读了Tkinter文档和问题Python Tkinter : Control/set the (Tab key) widget “visit” order和How to set the tab order in a tkinter application?以及其他一些问题,但没有找到答案。
这是工作示例:
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 小部件仍然无法选择。这个问题有解决方案吗?
答案 0 :(得分:0)
比例尺是可选的-我可以运行您的代码,并使用Tab键选择小部件。选择后,我可以使用箭头键来调整值。默认情况下,您无需执行任何操作即可获得该行为。
如果您要询问如何以视觉方式指示已选择它,请将highlightthickness
属性设置为大于零的值。
答案 1 :(得分:0)
解决了另一个与Scale
小部件有关的任务,我仔细地重读了Tkinterbook,发现其中的 config 选项takefocus
是这个问题(takefocus=1
)。另外,还需要将highlightthickness
选项设置为非零值,以明确选择,正如 Bryan Oakley 在他的回答中提到的那样。