简体   繁体   English

是否可以使用“Tab”键通过键盘在 Python 中选择 Tkinter 的 Scale 小部件?

[英]Is the Tkinter's Scale widget selectable in the Python by the keyboard with the 'Tab' key?

I select widgets in a window with the 'Tab' key.我使用“Tab”键在窗口中选择小部件。 I can select everything widgets which require some action (Entry, Checkbutton, Listbox, Buttons) except the Scale.我可以选择除 Scale 之外的所有需要​​操作的小部件(Entry、Checkbutton、Listbox、Buttons)。 Is it selectable in this way at all?可以这样选择吗?

I had already read Tkinter documentations and questions Python Tkinter : Control/set the (Tab key) widget “visit” order and How to set the tab order in a tkinter application?我已经阅读了 Tkinter 文档和问题Python Tkinter:控制/设置(Tab 键)小部件“访问”顺序以及如何在 tkinter 应用程序中设置 Tab 顺序? and some others and did not find an answer.和其他一些人并没有找到答案。

Here is the working example:这是工作示例:

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()

The result:结果:

在此处输入图片说明

I thought maybe event handlers for the Scale widget are required and made them (the placement is shown in the code above, the AAA position):我想可能需要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)

And their binding (the BBB position in the code):以及它们的绑定(代码中的 BBB 位置):

optionsWindow.bind('<Right>',keyRight)
optionsWindow.bind('<Left>',keyLeft)

The handlers work fine (I can change the shown "Scroll value" at any time with <- and -> keys) but the Scale widget remains unselectable.处理程序工作正常(我可以随时使用 <- 和 -> 键更改显示的“滚动值”),但Scale小部件仍然无法选择。 Has this problem a solution?这个问题有解决方案吗?

The scale is selectable - I can run your code and use the tab key to select the widget.比例是可选择的 - 我可以运行您的代码并使用 Tab 键来选择小部件。 Once selected, I can use the arrow keys to adjust the value.选择后,我可以使用箭头键调整值。 You get that behavior by default without having to do anything.默认情况下,您无需执行任何操作即可获得该行为。

If you're asking how to have a visual indication that it is selected, set the highlightthickness attribute to a value greater than zero.如果您要问如何有一个视觉指示它被选中,请将highlightthickness属性设置为大于零的值。

Solving another task, also concerned with the Scale widget, I reread the Tkinterbook carefully and found there the config option takefocus which is a decision of this problem ( takefocus=1 ).解决另一个任务,也与Scale小部件有关,我仔细阅读了Tkinterbook ,发现那里有配置选项takefocus ,这是这个问题的决定( takefocus=1 )。 Also one need to set the highlightthickness option to a non-zero value for the clear indication of the choise as Bryan Oakley mentioned alredy in his answer.还需要将highlightthickness选项设置为非零值,以明确表示Bryan Oakley在他的回答中提到的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM