0

I am trying to change color of label on a button click. But the label attribute is None.

The class below contains the label I am trying to change color.

class InfoGeneralFrame(Frame):
    COL_GREEN = "#179900"
    COL_RED = "#f70f02"
    COL_NETM = "#000"
    COL_BLUE_LT = "#049dc7"
    COL_BLUE = "#0940e6"

    rate_txt = 15
    rate_txt2 = 12
    name_txt = 9
    name_txt2 = 8
    pad_x = 10

    def __init__(self, tab, *args, **kwargs) -> None:
        super().__init__(tab, *args, **kwargs)

        # ** Some other label and other widgets**

        self.lbl_netm = Label(
            self,
            text="00.00%",
            font=("Halvetica", self.rate_txt, "bold"),
            foreground=self.COL_NETM,
            background="#fff",
        ).grid(row=3, column=0, sticky="ns", padx=self.pad_x)

The class that declared the above frame is below,

class TabGeneral(Frame):
    def __init__(self, container, *args, **kwargs) -> None:
        super().__init__(container, *args, **kwargs)

        self.log_msg = StringVar(self)
        self.var_brand = StringVar(self)
        self.var_category = StringVar(self)
        self.var_casetype = StringVar(self)

        self.frame1 = EntryGeneralFrame(self)
        self.frame2 = ButtonGeneralFrame(self)
        self.frame3 = InfoGeneralFrame(self)
        self.frame4 = LogFrame(self)

        self.log_msg.set("Message logging enabled")

        self.frame1.pack(pady=20)
        self.frame2.pack(pady=10)
        # self.frame3.pack(pady=10)  # Initially hidden
        self.frame4.pack(pady=25)

    def show_info_frame(self) -> None:
        self.frame3.pack(pady=10)
        self.frame4.pack_forget()
        if self.frame3.lbl_netm:
            self.frame3.lbl_netm.config(fg="#179900")
            print("Success")
        else:
            print("FAILED")

    def hide_info_frame(self) -> None:
        self.frame3.pack_forget()
        self.frame4.pack(pady=25)

I am trying to change the color of the Label lbl_netm from the function show_info_frame. The function is assigned for a button. But when I press the button, the lbl_netm is still None.

The main root app code: https://paste.pythondiscord.com/usifeyepiz.rb

  • It should. OP typed: `self.lbl_netm = Label(...).grid(...)`. You should separate the creation of the label and its grid assignment: `self.lbl_netm = Label(...)` and `self.lbl_netm.grid(...)`. For info, `Label()` returns the object, but `Label().grid()` sets the grid value and returns `None`, which is probably the cause of the error here. – JiyuuSensei Jul 14 '21 at 09:53
  • I didn't find the stack question related to that, cause I never thought that will be the error. Thanks for the quick response again ;-) – user9811098 Jul 14 '21 at 10:03

0 Answers0