尝试创建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”
答案 0 :(得分:1)
您为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)