【问题标题】:I want to map my tkinter buttons to keys on the numberpad, What am I doing wrong please?我想将我的 tkinter 按钮映射到数字键盘上的键,请问我做错了什么?
【发布时间】: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


【解决方案1】:
window.bind('/', back)
window.bind('*', current)
window.bind('-', next)

一个事件参数被传递给函数,所以给它们添加一个参数。

例子:

def back(event=None):
    ...

【讨论】:

    【解决方案2】:

    你没有绑定到任何特定的东西。你需要绑定一些东西。因此,在这种情况下,您希望绑定到根窗口,以便始终检测到键事件。

    您还需要更改在绑定中调用函数的方式。 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()
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      相关资源
      最近更新 更多