【问题标题】:Is the Tkinter's Scale widget selectable in the Python by the keyboard with the 'Tab' key?Tkinter 的 Scale 小部件是否可以通过键盘使用“Tab”键在 Python 中选择?
【发布时间】:2019-07-19 17:26:35
【问题描述】:

我使用“Tab”键在窗口中选择小部件。我可以选择所有需要操作的小部件(条目、检查按钮、列表框、按钮),但比例除外。完全可以这样选择吗?

我已经阅读了 Tkinter 文档和问题 Python Tkinter : Control/set the (Tab key) widget “visit” orderHow 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 小部件仍然无法选择。这个问题有解决办法吗?

【问题讨论】:

  • “可选择”是什么意思?您是在问如何添加某种视觉指示?
  • 是的,用于用户交互的视觉指示和激活。
  • 我在 Windows 上使用 3.7,我也无法通过 tab 键选择 Scale。虽然如果我在上面强制使用focus_set(),它确实会突出显示。
  • @HenryYik 你试过takefocus 选项吗?

标签: python python-3.x tkinter


【解决方案1】:

比例是可选的 - 我可以运行您的代码并使用 tab 键来选择小部件。选择后,我可以使用箭头键来调整值。默认情况下,您无需执行任何操作即可获得该行为。

如果您询问如何以视觉方式指示它已被选中,请将highlightthickness 属性设置为大于零的值。

【讨论】:

  • 我现在的机器上仍在使用 Python 3.4,也许这是个问题。周一我会在 Python 3.7 上试用它。谢谢!
  • @PeterNazarenko:我不明白你的评论。你是说设置highlightthickness 对你不起作用?
  • 是的,我在 Python 3.4 中尝试过 highlightthickness = 1 和 `= 2`,但仍然没有成功。我认为这不是 Python 版本(或不仅是)的问题,而是我当前的系统配置的问题,因为我刚刚尝试了另一个具有类似 GUI 控件的程序(USB 相机的驱动程序),并且这些滑块也适用于只用鼠标,不用按键。
  • 我在 Python 3.7 和 Windows 7 和 8.1 上测试过这个问题。没有takefocus=1,Tab 键不起作用。
【解决方案2】:

解决另一个任务,也与Scale 小部件有关,我仔细阅读了Tkinterbook 并在那里找到了 config 选项takefocus 这是这个问题的决定(takefocus=1 )。还需要将highlightthickness 选项设置为非零值,以便清楚地指示选择,因为 Bryan Oakley 在他的回答中已经提到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2013-10-10
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多