[英]Error when subclassing tk.Listbox (Attribute error object has no attribute 'tk')
嘗試創建 Listbox 的子類,以便我可以創建一個新的 KeyListbox
from tkinter import *
class KeyListbox(Listbox):
def __init__(self,parent,**kwargs):
super().__init__(self,parent,**kwargs)
root = Tk()
root.title("Key List Test")
testlist=[ "Python" , "Perl" , "C" , "PHP" , "JSP" , "Ruby"]
lijst = KeyListbox(root,selectmode='multiple')
for i in testlist:
lijst.insert(END,i)
lijst.pack(root)
root.mainloop()
AttributeError: 'KeyListbox' 對象沒有屬性 'tk'
您對super
使用了錯誤的語法。
class KeyListbox(Listbox):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
或者你可以像下面這樣調用父類:
class KeyListbox(Listbox):
def __init__(self, parent, **kwargs):
Listbox.__init__(self, parent, **kwargs)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.