0

I'm trying to get the elements located in the init of my class from another method in order to update a label. Here is an example code:

class mainApp:
    def __init__(self, master):
        my_frame = tk.Frame(master)
        my_frame.pack()
        label = tk.Label(master, text="Press the button")
        label.pack()

        btn = tk.Button(master, text="press",  command=ft.partial(self.pressed))
        btn.pack()
    def pressed(self)
        self.label.text = "Pressed"

The error I get is the following:

AttributeError: 'mainApp' object has no attribute 'label'

Is there a way to access this widgets from the init method of the class?

Thanks!

Varox
  • 333
  • 1
  • 5
  • 17
  • 2
    `self.label = tk.Label(master, text="Press the button")` and `self.label.configure(text="Pressed")` – Thingamabobs Dec 01 '22 at 10:53
  • 1
    Variables are only made available to the class members if you use the `self` keyword - you try to access `label` in your function `pressed` but only define it as a local variable in your `__init__` – mnikley Dec 01 '22 at 11:09

0 Answers0