2017-01-25 3 views
0

マイコード:バインディングキーへの機能が動作していない

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

アイデアはあるがdrawCheckboxを実行し、「確認」ボタンを押すのと同じことを行う入力/ Returnキーを押す持っています。これはまだ進行中ですが、removeInputStuffが実行されているときにEnterキーからdrawCheckbox関数をアンバインドします。それにもかかわらず、なぜ私はEnterキーを押しても、それが縛られている機能を実行しないのです。

答えて

1

キー(またはイベントの他の種類)に機能fctをバインドすると、関数はeventがイベント(マウス位置の種類に応じて、様々な属性を持つ、そのfct(event)のように一つの引数で呼び出され... )。あなたの問題は、あなたがdrawCheckboxを呼び出す関数は、

TypeError: drawCheckbox() takes 0 positional arguments but 1 was given

はあなたがいずれかの可能なデフォルト引数を使って関数を定義し、それを修正するので、Enterキーを押すたびに、それがエラーを発生させ、任意の引数を取らないということである

def drawCheckbox(event=None): 
    ... 

またはあなたが結合

master.bind('<Return>', lambda event: drawCheckbox()) 
を行うためにラムダ関数を使用することができます
関連する問題