python羊 发表于 2020-3-26 10:54:19

绑定键盘调用函数时会报错。

我想达到的效果是:
按下回车键触发函数。
但是下面这段代码,按下回车键会报错,
直接用鼠标点击BUTTON却一切正常,没有报错。

然后我将BUTTON绑定回车,也会报错
button.bind('<Button-1>', msg_demand)

command 调用函数正常,键盘调用却不行,请问为什么?

感谢感谢。


报错:————————————————————
    return self.func(*args)
TypeError: msg() takes 0 positional arguments but 1 was given
————————————————————————————————


原代码:

from tkinter import *

def msg_demand():
    print(entry.get())
   
root = Tk()
root.bind("<Return>",msg_demand)

button_search = Button(root, text=' O K ',command=msg_demand)
button_search.grid(padx=5,pady=3)

entry = Entry(root)
entry.grid()
entry.focus_set()

root.mainloop()

jinlovelive 发表于 2020-3-26 13:27:38

绑定按钮需要重新定义个函数,用于enter触发
def msg_demand_enter(self):msg_demand()

python羊 发表于 2020-3-26 14:06:57

jinlovelive 发表于 2020-3-26 13:27
绑定按钮需要重新定义个函数,用于enter触发
def msg_demand_enter(self):msg_demand()

感谢老哥
页: [1]
查看完整版本: 绑定键盘调用函数时会报错。