【问题标题】:How to change which tkinter Button is highlighted via arrow keys?如何更改通过箭头键突出显示的 tkinter 按钮?
【发布时间】:2018-12-06 08:28:48
【问题描述】:

我正在使用 python 中的 tkinter 库开发键盘应用程序。我做了一个键盘。我想要的是突出显示一个键,然后单击箭头键我想更改该突出显示的键。这是我的键盘代码。

from tkinter import *
import tkinter

Keyboard_App = tkinter.Tk()

def select(value):
    if value == "<-":
        input = entry.get("1.0", 'end-2c')
        entry.delete("1.0", END)
        entry.insert("1.0", input, END)

    elif value == " Space ":
        entry.insert(tkinter.END, ' ')

    elif value == "Tab":
        entry.insert(tkinter.END, '   ')

    else:
        entry.insert(tkinter.END, value)

buttons = [
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '=',
    'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '<-',
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '"',
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 'SHIFT',
    ' Space ',
]
entry = Text(Keyboard_App, width=97, height=8)
entry.grid(row=1, columnspan=15)

varRow = 2
varColumn = 0

for button in buttons:
    command = lambda x=button: select(x)
    if button != " Space ":
        tkinter.Button(Keyboard_App, text=button, width=5, bg="#000000", fg="#ffffff",
                       activebackground="#ffffff", activeforeground="#000000", relief="raised", padx=12,
                       pady=4, bd=4, command=command).grid(row=varRow, column=varColumn)

    if button == " Space ":
        tkinter.Button(Keyboard_App, text=button, width=60, bg="#000000", fg="#ffffff",
                       activebackground="#ffffff", activeforeground="#000000", relief="raised", padx=4,
                       pady=4, bd=4, command=command).grid(row=6, columnspan=16)

    varColumn += 1
    if varColumn > 10:
        varColumn = 0
        varRow += 1
Keyboard_App.mainloop()

这是我的键盘原图

我希望它像这样可以用箭头键移动。

【问题讨论】:

  • 是什么决定了键盘上的哪个键最初被突出显示(即,什么确定了初始位置)?
  • 您是否打算让用户也能够输入文本小部件?
  • @martineau 它可以是任意键。
  • 是的,我愿意@Bryan
  • 如果用户正在输入然后按下箭头键,您期望会发生什么?它会在文本小部件中移动光标,还是突出显示一个按钮?

标签: python tkinter tkinter-layout


【解决方案1】:

给你。我已将箭头键绑定到按钮。您可以使用它们进行导航。我相信基地已经准备好了,但您当然可以通过导航进行任何您想要的更改。

它的工作方式是将按钮存储在 2D 列表中,以便我可以通过其 2D 位置来引用它们。我将当前突出显示的按钮的位置(默认为 [0,0])存储在一个单独的变量中。按键时,当前按钮的边框设置为默认值,并突出显示下一个按钮。

from tkinter import *
import tkinter

Keyboard_App = tkinter.Tk()

buttons = [
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '=',
    'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '<-',
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '"',
    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 'SHIFT',
    ' Space ',
]
curBut = [-1,-1]
buttonL = [[]]
entry = Text(Keyboard_App, width=97, height=8)
entry.grid(row=0, columnspan=15)

varRow = 1
varColumn = 0

def leftKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 4:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [0,10]
        buttonL[0][10].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [curBut[0], (curBut[1]-1)%11]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def rightKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 4:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [curBut[0], (curBut[1]+1)%11]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def upKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 0:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]-1)%5, 0]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]-1)%5, curBut[1]]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def downKey(event):
    if curBut == [-1,-1]:
        curBut[:] = [0,0]
        buttonL[0][0].configure(highlightbackground='red')
    elif curBut[0] == 3:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]+1)%5, 0]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')
    else:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
        curBut[:] = [(curBut[0]+1)%5, curBut[1]]
        buttonL[curBut[0]][curBut[1]%11].configure(highlightbackground='red')

def select(value, x, y):
    if curBut != [-1,-1]:
        buttonL[curBut[0]][curBut[1]].configure(highlightbackground='#d9d9d9')
    curBut[:] = [x,y]
    buttonL[x][y].configure(highlightbackground='red')
    if value == "<-":
        input = entry.get("1.0", 'end-2c')
        entry.delete("1.0", END)
        entry.insert("1.0", input, END)

    elif value == " Space ":
        entry.insert(tkinter.END, ' ')

    elif value == "Tab":
        entry.insert(tkinter.END, '   ')

    else:
        entry.insert(tkinter.END, value)

for button in buttons:
    if button != " Space ":
        but = tkinter.Button(Keyboard_App, text=button, width=5, bg="#000000", fg="#ffffff", highlightthickness=4, 
                       activebackground="#ffffff", activeforeground="#000000", relief="raised", padx=12,
                       pady=4, bd=4, command=lambda x=button, i=varRow-1, j=varColumn: select(x, i, j))
        buttonL[varRow-1].append(but)
        but.grid(row=varRow, column=varColumn)

    if button == " Space ":
        but = tkinter.Button(Keyboard_App, text=button, width=60, bg="#000000", fg="#ffffff", highlightthickness=4, 
                       activebackground="#ffffff", activeforeground="#000000", relief="raised", padx=4,
                       pady=4, bd=4, command=lambda x=button, i=varRow-1, j=varColumn: select(x, i, j))
        buttonL[varRow-1].append(but)
        but.grid(row=6, columnspan=16)

    varColumn += 1
    if varColumn > 10:
        varColumn = 0
        varRow += 1
        buttonL.append([])

Keyboard_App.bind('<Left>', leftKey)
Keyboard_App.bind('<Right>', rightKey)
Keyboard_App.bind('<Up>', upKey)
Keyboard_App.bind('<Down>', downKey)
Keyboard_App.mainloop()

【讨论】:

  • @JawadMalik 当您按下任何箭头键时,这些键会突出显示。编辑了代码。现在,当您单击按钮时,它将突出显示。
  • @JawadMalik 我在你的程序中添加了一点代码,可以看到here
  • 如果需要帮助,我可以稍后联系您吗?
  • @JawadMalik 随时欢迎您在这里提问。祝你好运。
  • 我已经在另一篇文章中确认 windows 没有在 windows 10 上显示多个 Python 版本的亮点。所以这可能是操作系统/版本问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多