Binding a function to a key is not working

不羁岁月 提交于 2019-12-20 07:06:25

问题


My code:

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()

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.


回答1:


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() 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):
    ...

or you can use a lambda function to do the binding

master.bind('<Return>', lambda event: drawCheckbox())


来源:https://stackoverflow.com/questions/41848530/binding-a-function-to-a-key-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!