Home > Software engineering >  How can I update labels which are located different windows synchronically in tkinter?
How can I update labels which are located different windows synchronically in tkinter?

Time:10-04

How can update labels located different windows and different python scripts in tkinter. I used TopLevel() and global for this change but I cannot delete previous text which is written on the label. When I used config() I see this error:

STEPI_LABEL =tk.config(text=f"Bad Character {total}", font=("Arial", 24, "bold")) AttributeError: module 'tkinter' has no attribute 'config'

page 1:

STEPI_LABEL.grid(column=19,row=4)
STEPII_LABEL=Label(text="I am a label",font=("Arial",24,"bold"))
STEPII_LABEL.config(text="STEPII_LABEL")

page2: (OOP class)

global STEPI_LABEL

            STEPI_LABEL =tk.config(text=f"Bad Character {total}", font=("Arial", 24, "bold"))
            STEPI_LABEL.grid(column=19, row=4)

CodePudding user response:

The reason tk itself has no attribute config is because it is not a widget; it is a module. Only widgets (like Tk, Label, TopLevel, etc.) have a config() function. The config() function is used to change widgets that already exist; it cannot be used to create widgets. In your code, you try to create STEPI_LABEL by using config(); use Label() instead:

global STEPI_LABEL

STEPI_LABEL = tk.Label(text=f"Bad Character {total}", font=("Arial", 24, "bold"))
STEPI_LABEL.grid(column=19, row=4)

If you want to change the label later (for instance, change it's text) then you can use config():

STEPI_LABEL.config(text=f"Not a bad character {total}")

Any arguments that can be passed to Label() on creation, except for master, can be used in config() as well.

  • Related