簡體   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