Reputation: 312
I have written a class to create Listbox objects, I want to bind the list box with a method in the same class but I am getting attribute error. what am I doing wrong here??
class ListObj(tkinter.Listbox):
def __init__(self, window, cname, r, c, rs, cs, sticky, bg, padx=5, pady=5, ipadx=0, ipady=0, **kwargs):
self = tkinter.Listbox(window)
self.grid(row=r, column=c, rowspan=rs, columnspan=cs, sticky=sticky, padx=padx, pady=pady,
ipadx=ipadx, ipady=ipady)
self.bind('<<ListboxSelect>>', self.on_select)
def on_select(self):
pass
output:
AttributeError: 'Listbox' object has no attribute 'on_select'
Upvotes: 0
Views: 31
Reputation: 15226
The line causing the problem is self = tkinter.Listbox(window)
. You do not need to tell self it is a listbox because it already inherits listbox in the class definition.
Do this instead:
class ListObj(tkinter.Listbox):
def __init__(self, window, **kwargs):
super().__init__()
self.bind('<<ListboxSelect>>', self.on_select)
def on_select(self):
pass
You should use grid on the reference variable outside of the class. It is not the best option to use a geometry manager from inside of a class.
var_name = ListObj(var1, var2, var2 ...)
var_name.grid(configs....)
Upvotes: 1