Wim Stockman
Wim Stockman

Reputation: 59

Error when subclassing tk.Listbox (Attribute error object has no attribute 'tk')

Trying to create a subclass of Listbox so I could make a new 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' object has no attribute 'tk'

Upvotes: 0

Views: 92

Answers (1)

Henry Yik
Henry Yik

Reputation: 22493

You used the wrong syntax for super.

class KeyListbox(Listbox):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)

Or you can call the parent class like below:

class KeyListbox(Listbox):
    def __init__(self, parent, **kwargs):
        Listbox.__init__(self, parent, **kwargs)

Upvotes: 1

Related Questions