kylieCatt
kylieCatt

Reputation: 11049

Creating A Custom Listbox - Tkinter

I am messing around with tkinter and I am trying to create a custom listbox. The code I have so far works but I would like to set a different default width for for this widget but I can't figure out how to do it. What I have now returns this error:

AttributeError: 'ShelfListbox' object has no attribute 'tk'

The code:

from tkinter import *
from TestData import *
from Item import *
from Shelf import ShelfListbox

def create_item(lb):
    global item
    item = Item(sku_ent.get(), title_ent.get(), qty_ent.get())
    lb.insert(END, item.print_item())

root = Tk()
frame = Frame(root)

sku_lbl = Label(root, text="SKU: ").grid()
sku_ent = Entry(root)
sku_ent.grid(row=0, column=1)

title_lbl = Label(root, text="Title: ").grid(row=1, column=0)
title_ent = Entry(root)
title_ent.grid(row=1, column=1)

qty_lbl = Label(root, text="QTY: ").grid(row=2, column=0)
qty_ent = Entry(root)
qty_ent.grid(row=2, column=1)

item_list = ShelfListbox(root)
item_list.grid(row=0, rowspan=3, column=2)

for key in items:
    item_list.insert(END, items[key].print_item()) #ERROR COMES FROM THIS LINE

btn = Button(root, text="Confirm", command=lambda: create_item(item_list)).grid(row=3, columnspan=2)

root.mainloop()

The ShelfListbox code:

from tkinter import *


class ShelfListbox(Listbox):
    def __init__(self, master):
        Listbox.__init__(master, width=60)

Upvotes: 0

Views: 3876

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

You need to pass self as the first parameter to the Listbox.__init__ method:

Listbox.__init__(self, master, width=60)

Upvotes: 2

Related Questions