Home > Blockchain >  Trying to access widget from other class tkinter
Trying to access widget from other class tkinter

Time:11-01

When I am trying to reference in a different class in a function I get a error saying their is not attribute between the widget I am referencing and the class. Here is the code displaying the referencing:

class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.init_csv()


    def init_csv(self):
        LeftFrame.spellings_listbox.insert(tk.END,i)

class LeftFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container,width=400,height=600,bg="red")
        self.pack(side=tk.LEFT)
        self.pack_propagate(0)
        self.widgets()

    def widgets(self):

        self.spellings_listbox = tk.Listbox(self)
        self.spellings_listbox.pack(expand=True,fill=tk.BOTH,side=tk.BOTTOM)

CodePudding user response:

You have to create an instance of the LeftFrame class before you can use it or the widgets inside. This isn't something unique to tkinter, it's a fundamental aspect of using classes.

Usually the solution looks something like this:

class Window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.init_csv()
        self.left_frame = LeftFrame(self)


    def init_csv(self):
        self.left_frame.spellings_listbox.insert(tk.END,i)
  • Related