我正在使用tkinter
,我正在尝试创建自定义列表框。我到目前为止的代码工作,但我想为这个小部件设置不同的默认宽度,但我无法弄清楚如何做到这一点。我现在拥有的内容会返回此错误:
AttributeError: 'ShelfListbox' object has no attribute 'tk'
代码:
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()
ShelfListbox代码:
from tkinter import *
class ShelfListbox(Listbox):
def __init__(self, master):
Listbox.__init__(master, width=60)
答案 0 :(得分:2)
您需要将self
作为第一个参数传递给Listbox.__init__
方法:
Listbox.__init__(self, master, width=60)