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
#3
You could make pop_label a global
def ACwindowdriver():
    global pop, pop_label
but it would be far better to make it an attribute of the class.

Actually, I wouldn't expose the labels at al.I would instead create a tkinter.IntVariable, and use this to set the text of the labels.
import tkinter as tk

class PopupWindow(tk.Toplevel):
    def __init__(self, parent, count):
        super().__init__(parent, bg="black")
        self.overrideredirect(True)
        self.count = count
 
        tk.Label(
            self,
            textvariable=count,
            bg="green",
            fg="white",
            font=("helvetica", 12),
            width=3).pack(padx=5, pady=5)
 
        tk.Button(
            self,
            text=" Up ",
            command=lambda: self.increment(1),
            bg="black",
            fg="green").pack(padx=5, pady=5)
 
        tk.Button(
            self,
            text="Down",
            command=lambda: self.increment(-1),
            bg="black",
            fg="green").pack(padx=5, pady=5)

    def increment(self, amount):
        self.count.set(self.count.get() + amount)


class RootWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Root Window")
        self.popups = []
        self.count = tk.IntVar(self, 20)

        tk.Label(self, textvariable=self.count, width=5) \
            .pack(padx=5, pady=5, side=tk.LEFT)
 
        tk.Button(self, text="Destroy All Popups", command=self.destroy_popups) \
            .pack(padx=5, pady=5, side=tk.LEFT)

        tk.Button(self, text="Create New Popup", command=self.popup) \
            .pack(padx=5, pady=5, side=tk.LEFT)


    def popup(self):
        """Create a popup window"""
        id = len(self.popups)
        popup = PopupWindow(self, self.count")
        self.popups.append(popup)
        x = (id % 5) * 120
        y = (id // 5) * 120
        popup.geometry(f"{120}x{120}+{x}+{y}")

    def destroy_popups(self):
        """Destroy all the popup windows"""
        for pop in self.popups:
            pop.destroy()
        self.popups = []

RootWindow().mainloop()
tompranks likes this post
Reply


Messages In This Thread
RE: Can't update label in new tk window, object has no attribute - by deanhystad - Aug-27-2022, 02:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to update label text using a grid button. Edward_ 7 1,971 Dec-18-2024, 03:05 AM
Last Post: Edward_
  update text variable on label with keypress knoxvilles_joker 5 7,977 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,296 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,795 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,710 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 24,546 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 15,627 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