【发布时间】:2020-03-12 20:26:15
【问题描述】:
这里是 Python 菜鸟, 我有一个自动售货机的零食目录、文本到语音,以及一个后退、下一个和当前按钮。
我想将我的按钮映射到数字键盘上的按键,但它似乎不起作用。当 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()
【问题讨论】:
-
你需要绑定一些东西,而不是单独的
bind()。window.bind()等等。back.func也无效。只需back就可以了。next也是一个内置名称,所以很可能是该函数的名称。
标签: python tkinter keymapping