Creating a listbox with scroll and buttons. Getting an AttributeError: 'str' object has no attribute 'tk'
Hi I am pretty new to Tkinter. I am trying to create a listbox with a scroll bar and a couple buttons but I am getting an Attribute error. It seems somewhere I have overwritten master. Please help:
class advancedListBox:
def __init__(self, master, listBoxList, label, callbackFunction = None):
self.listBoxList = listBoxList
self.label = label
self.callbackFunction = callbackFunction
self.entryVar = tk.StringVar()
self.entryVar.set("Search")
self.entry = tk.Entry(master)
self.entry.config(textvariable = self.entryVar)
self.entry.grid(row = 0,sticky= "E"+"W")
self.entry.bind('<KeyPress>', self.listBox_On_keypress)
self.entry.bind('<KeyRelease>', self.listBox_On_keyrelease)
self.entryLabel = tk.Label(self.label)
self.entryLabel.grid(row = 1, column = 0, padx =(0,18))
self.listBox = tk.Listbox(master)
self.listBox.grid(row = 2, rowspan = 3, column = 0, sticky= "N"+"E"+"S"+"W")
self.listBox.bind('<<ListboxSelect>>', on_select)
self.listbox_update(self.listBoxList)
self.scrollbar = tk.Scrollbar(master, orient="vertical")
self.scrollbar.config(command=self.listBox.yview)
self.scrollbar.grid(row = 2, column = 1,rowspan = 3, sticky= "N"+"S")
self.listBox.config(yscrollcommand=scrollbar.set)
self.deleteButton = tk.Button(self.master, text="Delete",
command=lambda lb=architecturesListBox: deleteFromListBox(lb,tk.ANCHOR))
self.deleteButton.grid(row =5,sticky= "E"+"W"
)
Traceback (most recent call last):
File "c:/Users/Jerwin/Desktop/Jadon's Stuff/Jadon's Python Programs/List Boxes with delete and scrollbar.py", line 110, in architecturesListBox = advancedListBox(root, architectures,"Name -- Pictures")
File "c:/Users/Jerwin/Desktop/Jadon's Stuff/Jadon's Python Programs/List Boxes with delete and scrollbar.py", line 22, in init self.entryLabel = tk.Label(self.label)
File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2766, in init Widget.init(self, master, 'label', cnf, kw)
File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2292, in init BaseWidget._setup(self, master, cnf) File "C:\Users\Jerwin\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py", line 2262, in _setup self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
Solution 1:
self.label
is a string. You're using it as the parent for a label when you do self.entryLabel = tk.Label(self.label)
. A string can't be the parent of a widget.
The first positional argument when creating a widget must be another widget, except in the case of creating the initial root window.