简体   繁体   中英

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. I can select everything widgets which require some action (Entry, Checkbutton, Listbox, Buttons) except the Scale. 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? 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):

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

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. Has this problem a solution?

The scale is selectable - I can run your code and use the tab key to select the widget. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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