2016-04-25 87 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