0

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)
1
  • can you show the whole stack trace? There's not enough information, and I'm not convinced the error is coming from the line you say it does, since that line would likely give you a different error. Commented Jan 1, 2014 at 21:05

1 Answer 1

2

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

Listbox.__init__(self, master, width=60)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.