Tkinter绑定功能

时间:2016-04-25 08:05:11

标签: python tkinter bind

我有这个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'对象不可调用”??

1 个答案:

答案 0 :(得分:2)

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