繁体   English   中英

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'对象不可调用”的信息?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM