0

我正在尝试在单击按钮时更改标签的颜色。但是标签属性是None.

下面的类包含我要更改颜色的标签。

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)

声明上述框架的类如下,

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)

我正在尝试lbl_netm从函数中更改标签的颜色show_info_frame。该功能被分配给一个按钮。但是当我按下按钮时,lbl_netm它仍然是None.

主根应用代码:https ://paste.pythondiscord.com/usifeyepiz.rb

4

0 回答 0