将功能绑定到键不起作用 [英] Binding a function to a key is not working
问题描述
我的代码:
import tkinter
master = tkinter.Tk()
master.title("test1")
master.geometry("300x300")
masterFrame = tkinter.Frame(master)
masterFrame.pack(fill=tkinter.X)
checkboxArea = tkinter.Frame(masterFrame, height=26)
checkboxArea.pack(fill=tkinter.X)
inputStuff = tkinter.Frame(masterFrame)
checkboxList = []
def drawCheckbox():
checkboxList.append(entry.get())
entry.delete(0,tkinter.END)
checkboxRow = tkinter.Frame(checkboxArea)
checkboxRow.pack(fill=tkinter.X)
checkbox1 = tkinter.Checkbutton(checkboxRow, text = checkboxList[-1])
checkbox1.pack(side=tkinter.LEFT)
deleteItem = tkinter.Button(checkboxRow, text = "x", command=checkboxRow.destroy, bg="red", fg="white", activebackground="white", activeforeground="red")
deleteItem.pack(side=tkinter.RIGHT)
def bindToEnter():
master.bind('<Return>', drawCheckbox)
def createInputStuff():
paddingFrame = tkinter.Frame(inputStuff, height=5)
paddingFrame.pack(fill=tkinter.X)
buttonDone.pack()
inputStuff.pack()
buttonAdd.pack_forget()
bindToEnter()
def removeInputStuff():
inputStuff.pack_forget()
buttonAdd.pack()
buttonDone.remove()
buttonDone = tkinter.Button(inputStuff, text = "Close Input", command=removeInputStuff)
buttonAdd = tkinter.Button(masterFrame, text="Add Item", command=createInputStuff)
buttonAdd.pack()
topInput = tkinter.Frame(inputStuff)
bottomInput = tkinter.Frame(inputStuff)
topInput.pack()
bottomInput.pack()
prompt = tkinter.Label(topInput, text="What do you want your checkbox to be for?")
prompt.pack()
entry = tkinter.Entry(bottomInput, bd=3)
entry.pack(side=tkinter.LEFT)
buttonConfirm = tkinter.Button(bottomInput, text="Confirm", command=drawCheckbox)
buttonConfirm.pack(side=tkinter.LEFT)
master.mainloop()
这个想法是让按下Return/Enter的操作与点击确认"按钮,运行drawCheckbox的操作相同.这项工作仍在进行中,我将在运行removeInputStuff时将EnterCheck键与drawCheckbox函数解除绑定.但是,我仍然不明白为什么按Enter键不能运行它所绑定的功能.
The idea is to have pressing Return/Enter do the same thing as hitting the "Confirm" button, running drawCheckbox. This is still a work in progress, I'll unbind the drawCheckbox function from the Enter key when removeInputStuff is run. Nevertheless, I still don't get why pressing the Enter key doesn't run the function it's bound to.
推荐答案
当您将函数fct
绑定到键(或任何其他类型的事件)时,该函数将使用一个像fct(event)
这样的参数进行调用, event
根据事件的类型(鼠标位置等)具有各种属性.您的问题是调用drawCheckbox
的函数没有任何参数,因此每次按Enter都会引发错误
When you bind a function fct
to a key (or any other kind of event), the function is called with one argument like that fct(event)
, event
having various attributes depending on the kind of event (mouse position, ...). Your problem is that the function you call drawCheckbox
does not take any argument, so every time you press Enter, it raises an error
TypeError:drawCheckbox()接受0个位置参数,但给出了1个
TypeError: drawCheckbox() takes 0 positional arguments but 1 was given
要更正它,您可以使用默认参数定义函数,
To correct it you can either define your function with a default argument,
def drawCheckbox(event=None):
...
或者您可以使用lambda函数进行绑定
or you can use a lambda function to do the binding
master.bind('<Return>', lambda event: drawCheckbox())
这篇关于将功能绑定到键不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!