简体   繁体   中英

Binding a function to a key is not working

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM