0

I have an annoying issue. I am trying to create class-based front panel for my application to keep things tidy, but I am having an issue with updating fields in sub-class windows.

In the following code, "main_lbl" is updated correctly, but "dev_lbl", inside Frame_1, is not.

class Frame_1(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        ...
        self.dev_lbl = tk.Label(self, text="__init__")
        self.dev_lbl.pack(side="top", fill="x", expand=1)

class MainApp(tk.Tk):
    def __init__(self, master=None, title="MAIN", size="222x333+100+100"):
        super().__init__()

        EF = Frame_1(self).pack(side="top", padx=2, pady=2, fill="x", expand=True)
        self.main_lbl.tk.Label(self, text="Main init")
        self.main_lbl.pack(side="bottom")
        #<RUN mainloop()>
        self.update_GUI()
        self.mainloop()

    def update_GUI(self):
        update_delay = 10
        self.main_lbl["text"] = "updated"
        Frame_1(self).dev_lbl["text"] = "updated"
        self.after(update_delay, self.update_GUI)

root = MainApp(title="BRIDGE")

I have also tried using tk.StringVar() ... same result.

Advice?

Thank you, Radovan

PS: Actually, I would like the display to be in the separate window. I tried tk.Toplevel:

class ShowVariables(tk.Toplevel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent

        self.all_dev_lbl = tk.Label(self.Bot_frame, text="__init__")
        self.all_dev_lbl.pack(side="top", fill="x", expand=1)

but that did not work either.

  • Does this answer your question? [how-would-i-access-variables-from-one-class-to-another](https://stackoverflow.com/questions/19993795) – stovfl Jun 19 '20 at 17:27

1 Answers1

1

You need to use the actual instance of Frame_1, and to do that you need to save a reference:

class MainApp(tk.Tk):
    def __init__(self, master=None, title="MAIN", size="222x333+100+100"):
        super().__init__()
        ...
        self.EF = Frame_1(self)
        self.EF.pack(side="top", padx=2, pady=2, fill="x", expand=True)
        ...

Once you have the reference, you can use it to update attributes of the object:

def update_GUI(self):
    ...
    self.EF.dev_lbl["text"] = "updated"
    ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Darn! So simple. I tried to be too efficient with my one-liner. Thanks! – Radovan Urban Jun 19 '20 at 16:20
  • Would you happen to know if text can be continuously updated in the Toplevel window (see PS in my post)? I can either get a static text or entire new Toplevel window every update cycle ... neither is ideal ... Thanks. – Radovan Urban Jun 19 '20 at 16:35
  • @RadovanUrban: yes, you can update text in a label continuously, both in a toplevel and somewhere inside the root window. – Bryan Oakley Jun 19 '20 at 17:12
  • *@brian-oakley* I am still getting static text. I might need the reference in the root window just like with the frame ... need to think about it ... still learning :-). Cheers! – Radovan Urban Jun 19 '20 at 17:32