我想将我的 tkinter 按钮映射到数字键盘上的键,请问我做错了什么?
I want to map my tkinter buttons to keys on the numberpad, What am I doing wrong please?
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()
window.bind('/', back)
window.bind('*', current)
window.bind('-', next)
一个事件参数被传递给函数,所以给它们添加一个参数。
示例:
def back(event=None):
...
您不绑定任何特定内容。你需要绑定一些东西。所以在这种情况下你想绑定到根 window 以便始终检测到按键事件。
您还需要更改在绑定中调用函数的方式。 back.func
不正确。而只是使用 back
.
接下来更改您的函数的名称 next
,因为这已经是 Python 的内置名称,因此您要覆盖这个有用的方法。
接下来,这些函数中的每一个都需要至少一个参数来处理从绑定发送到函数的事件。如果您还需要从其他地方调用该函数而不发送参数,您可以使用 event=None
来处理调用该函数的任何内容。
接下来将所有导入项放在顶部。您应该始终将导入放在首位。
这是您的代码的清理版本:
import tkinter
import pyttsx3
# import sys # this import is not used.
vl = ["donuts", "cookies", "spicy chips", "mild chips", "cheesy chips",
"mini donuts", "Mrs. Freshlys Cupcakes", "rubbery cake thing"]
engine = pyttsx3.init()
cupo = vl[0] # cupo is current position, 0 is the first entry in the vl list
def current(event=None):
global cupo # cupo was defined outside of the function, therefore we call global
engine.say(cupo)
engine.runAndWait()
def back(event=None):
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_func(event=None):
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()
def vendy():
tkinter.Label(window, text="Vendy!").pack()
print('\n'.join(map(str, vl)))
window = tkinter.Tk()
window.title("GUI")
tkinter.Button(window, text="Back", command=back).pack()
tkinter.Button(window, text="Repeat", command=current).pack()
tkinter.Button(window, text="Next", command=next_func).pack()
window.bind('/', back)
window.bind('*', current)
window.bind('-', next_func)
window.mainloop()
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()
window.bind('/', back)
window.bind('*', current)
window.bind('-', next)
一个事件参数被传递给函数,所以给它们添加一个参数。
示例:
def back(event=None):
...
您不绑定任何特定内容。你需要绑定一些东西。所以在这种情况下你想绑定到根 window 以便始终检测到按键事件。
您还需要更改在绑定中调用函数的方式。 back.func
不正确。而只是使用 back
.
接下来更改您的函数的名称 next
,因为这已经是 Python 的内置名称,因此您要覆盖这个有用的方法。
接下来,这些函数中的每一个都需要至少一个参数来处理从绑定发送到函数的事件。如果您还需要从其他地方调用该函数而不发送参数,您可以使用 event=None
来处理调用该函数的任何内容。
接下来将所有导入项放在顶部。您应该始终将导入放在首位。
这是您的代码的清理版本:
import tkinter
import pyttsx3
# import sys # this import is not used.
vl = ["donuts", "cookies", "spicy chips", "mild chips", "cheesy chips",
"mini donuts", "Mrs. Freshlys Cupcakes", "rubbery cake thing"]
engine = pyttsx3.init()
cupo = vl[0] # cupo is current position, 0 is the first entry in the vl list
def current(event=None):
global cupo # cupo was defined outside of the function, therefore we call global
engine.say(cupo)
engine.runAndWait()
def back(event=None):
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_func(event=None):
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()
def vendy():
tkinter.Label(window, text="Vendy!").pack()
print('\n'.join(map(str, vl)))
window = tkinter.Tk()
window.title("GUI")
tkinter.Button(window, text="Back", command=back).pack()
tkinter.Button(window, text="Repeat", command=current).pack()
tkinter.Button(window, text="Next", command=next_func).pack()
window.bind('/', back)
window.bind('*', current)
window.bind('-', next_func)
window.mainloop()