2014-04-14 50 views
1

我是Tkinter的新手。爲了學習Tkinter,我遵循this tutorial,我試圖用它來做我的新小項目。但是,當我在我創建的輸入框上按下輸入時出現錯誤。這裏是我的代碼:Tkinter錯誤,用函數綁定一個入口對象,給出2個參數,需要1個

# -*- coding: utf-8 -*- 
import Tkinter 

class PocketDEX(Tkinter.Tk): 
    def __init__(self, parent): 
     Tkinter.Tk.__init__(self, parent) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     self.grid() 

     self.labelVariable = Tkinter.StringVar() 
     self.labelVariable.set("Cuvântul căutat este:") 
     label = Tkinter.Label(self, textvariable = self.labelVariable, anchor="w") 
     label.grid(column = 0, row = 0) 

     self.entry = Tkinter.Entry(self) 
     self.entry.grid(column = 1, row = 0, sticky='EW') 
     self.entry.bind("<Return>", self.EnterPressed) 

    def EnterPressed(self): 
     print "It worked!" 


if __name__ == "__main__": 
    app = PocketDEX(None) 
    app.title('★ PocketDEX ★') 
    app.mainloop() 

我得到的錯誤是:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__ 
    return self.func(*args) 
TypeError: EnterPressed() takes exactly 1 argument (2 given) 

我不exaclty明白爲什麼給定的,我使用Python 2.7.6 2個參數在Windows 8.1 Pro

回答

2

另一個參數是event。這個是正常的。該事件具有可以訪問的與其關聯的屬性。例如:

event.keysym 
>>> 'Return' 

來處理,這是剛纔修改的回調方法接受事件參數的最簡單方法:

def EnterPressed(self, event): 
    print "It worked!" 

更多信息:http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

+0

感謝,它的工作! – MarK

相關問題