我想将我的Tkinter按钮映射到数字键盘上的键,请问我在做什么错?

问题描述 投票:-1回答:1

Python noob在这里,我有自动售货机的零食目录,文本到语音以及后退,下一个和当前按钮。

我想将我的按钮映射到数字键盘上的键,但是它似乎不起作用。当gui弹出时,我可以单击按钮,它将为我读取列表中的项目,但是我希望能够使用数字键盘来控制它,而不是使用鼠标来单击按钮。

vl = ["donuts","cookies","spicy chips","mild chips","cheesy chips","mini donuts","Mrs. Freshlys Cupcakes","rubbery cake thing"]
import pyttsx3
engine = pyttsx3.init()
cupo = vl[0] # cupo is current position, 0 is the first entry in the vl list
def current():
       global cupo # cupo was defined outside of the function, therefore we call global
       engine.say(cupo)
       engine.runAndWait()


def back():
        global cupo
        pos = vl.index(cupo)
        if pos == 0: # pos is position
                engine.say(cupo)
                engine.runAndWait()
        else:
                prepo = int(pos) - 1 # prepo is previous position
                cupo = vl[prepo]
                engine.say(cupo)
                engine.runAndWait()


def next():
        global cupo
        pos = vl.index(cupo)
        if pos == (len(vl) - 1):
                engine.say(cupo)
                engine.runAndWait()
        else:
                nexpo = int(pos) + 1 # nexpo is next position
                cupo = vl[nexpo]
                engine.say(cupo)
                engine.runAndWait()

print('\n'.join(map(str,vl)))

import tkinter
import sys




window = tkinter.Tk()
window.title("GUI")

def vendy():
    tkinter.Label(window, text = "Vendy!").pack()

b1 = tkinter.Button(window, text = "Back", command = back).pack()
b2 = tkinter.Button(window, text = "Repeat", command = current).pack()
b3 = tkinter.Button(window, text = "Next", command = next).pack()

bind('/',back.func)
bind('*',current.func)
bind('-',next.func)

window.mainloop()

python tkinter keymapping
1个回答
-1
投票
window.bind('/', back)
window.bind('*', current)
window.bind('-', next)
© www.soinside.com 2019 - 2024. All rights reserved.