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()
window.bind('/', back)
window.bind('*', current)
window.bind('-', next)