Tkinter 绑定函数

Tkinter bind function

我有这个 python 代码打印写入提示的文本:

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' object is not callable" ??

您在 CommandList class 的第一行中将 show 定义为等于 False 的布尔值,但无论如何都没有使用它。现在,当您有一个 CommandList 对象时,show() 会尝试调用您定义的 class 级别的 bool,而不是方法。