Python Forum
[Tkinter] Can't update label in new tk window, object has no attribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Can't update label in new tk window, object has no attribute
#2
The short answer is the pop_label only exists locally to ACwindowdriver
using global in GUI code is not ideal, I prefer to code GUI using classes.

Note: see Namespace flooding with * imports

The code rewritten answer, using classes and an IntVar
import tkinter as tk


class AcWindowDriver(tk.Toplevel):
    def __init__(self, count, *args, **kwargs):
        super().__init__(*args, *kwargs)
        self.title("My Popup")
        self.geometry("150x200+00+00")
        self.overrideredirect(True)  # Borderless
        self.config(bg="black")
        self.count = count
        pop_label = tk.Label(
            self, bg="green", fg="white", font=("helvetica", 12), textvariable=count
        )
        pop_label.config(text=count)
        pop_label.grid(row=1, column=0, pady=10)
        my_frame = tk.Frame(self, bg="black")
        my_frame.grid(row=1, column=1, pady=10)
        heatdriver_btn = tk.Button(
            my_frame, command=self._on_heatdriver_btn, bg="black"
        )
        heatdriver_btn.grid(row=2, column=2, pady=10)
        colddriver_btn = tk.Button(
            my_frame, command=self._on_colddriver_btn, bg="black"
        )
        colddriver_btn.grid(row=3, column=2, pady=10)

    def _on_heatdriver_btn(self):
        current_count = self.count.get()
        self.count.set(current_count + 1)

    def _on_colddriver_btn(self):
        current_count = self.count.get()
        self.count.set(current_count - 1)


class MainFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, *kwargs)
        self.count = tk.IntVar(self, 20)
        self.ac_window_driver = None
        destroy_btn = tk.Button(self, text="destroy", command=self._on_destroy_btn)
        destroy_btn.place(x=1, y=1)
        ac_driver_btn = tk.Button(
            self, text="ACwindowdriver", command=self._on_ac_driver_btn
        )
        ac_driver_btn.pack(pady=50)
        my_label = tk.Label(self, textvariable=self.count)
        my_label.pack(pady=20)

    def _on_destroy_btn(self):
        if not self.ac_window_driver:
            return
        self.ac_window_driver.destroy()
        self.ac_window_driver = None

    def _on_ac_driver_btn(self):
        if self.ac_window_driver:
            return
        self.ac_window_driver = AcWindowDriver(self.count, self)


def main():
    app = tk.Tk()
    app.geometry("300x300")
    main_frame = MainFrame(app)
    main_frame.pack()
    app.mainloop()


if __name__ == "__main__":
    main()
tompranks likes this post
Reply


Messages In This Thread
RE: Can't update label in new tk window, object has no attribute - by Yoriz - Aug-27-2022, 11:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to update label text using a grid button. Edward_ 7 1,972 Dec-18-2024, 03:05 AM
Last Post: Edward_
  update text variable on label with keypress knoxvilles_joker 5 7,980 May-31-2024, 02:09 PM
Last Post: menator01
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 2,804 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  'NoneType' object has no attribute 'get' zunebuggy 8 8,309 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 1,939 Aug-20-2023, 04:45 PM
Last Post: menator01
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 4,799 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  PysimpleGUI window update dynamically SamLiu 6 6,019 Apr-05-2023, 02:32 PM
Last Post: SamLiu
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 3,711 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 24,555 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 15,629 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020