2016-04-25 78 views
1

我有打印的文字寫入提示這個Python代碼:的Tkinter綁定功能

from Tkinter import * 

class CommandList(object): 
    show = False 
    def __init__(self): 
     self.show = False 

    def show(self): 
     print "showed" 

    def hide(self): 
     self.show = False 


    def is_showed(self): 
     return self.show 


master = Tk() 
tab = CommandList() 



e = Entry(master, width=1000) 
e.pack() 

def enter(event): 
    master.quit() 
def escape(event): 
    exit() 
def tabulator(tab): 
    print type(tab) 
    tab.show() 


e.bind('<Control_L>j', enter) 
e.bind('<Return>', enter) 
e.bind('<Escape>', escape) 

e.bind('<Tab>', lambda event, tab=tab: tabulator(tab)) 

e.focus_set() 
master.mainloop() 
print e.get() 


它工作正常,但 當我按下Tab鍵,所以我得到的錯誤:

<class '__main__.CommandList'> 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__ 
    return self.func(*args) 
    File "stack-question.py", line 41, in <lambda> 
    e.bind('<Tab>', lambda event, tab=tab: tabulator(tab)) 
    File "stack-question.py", line 34, in tabulator 
    tab.show() 
TypeError: 'bool' object is not callable 

我看到該選項卡類型CommandList,所以爲什麼我得到「TypeError:'bool'對象不可調用?」?

回答

2

您將show定義爲與您的CommandList類中的第一行等於False的布爾值,然後不反正使用它。現在,當您有一個CommandList對象時,show()會嘗試調用您定義的類級別布爾值,而不是該方法。

+0

非常感謝。我忽略了這個愚蠢的錯誤... – LegnaRuoy