gpt4 book ai didi

python - Tkinter 绑定(bind)函数

转载 作者:行者123 更新时间:2023-12-01 03:59:35 27 4
gpt4 key购买 nike

我有这个 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 类中的第一行将 show 定义为等于 False 的 bool,但无论如何都没有使用它。现在,当您有一个 CommandList 对象时,show() 会尝试调用您定义的类级 bool,而不是方法。

关于python - Tkinter 绑定(bind)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com