继承tk.Listbox时发生错误(属性错误对象没有属性“ tk”)

时间:2019-06-18 09:07:29

标签: python tkinter listbox subclass

尝试创建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”

1 个答案:

答案 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)