user2933060
user2933060

Reputation: 17

How to alter attributes in Tkinter

Is there any way to alter options of a widget after creating/drawing it? I can't seem to find any way to do so. What I'm currently aiming for is altering the fg of a Label once its temp0 textvariable is >= 50.

This code is part of a bigger program, so I didn't want to put all of that here, since the essential part is that I am not sure how to change the fg (i.e. font color) for that Label once I get the b[0] value and find out that it is above 50. Is the self.t0.config(fg="red") the proper syntax for that?

 class App:
    def __init__(self, master):

    #live updating TkInter variables    
        self.temp0 = DoubleVar()

        frame = Frame(master)
        self.t0 = Label(frame, fg="blue", textvariable=self.temp0,font=(20)).grid(row=2, column=0)
        frame.pack(padx=10, pady=10)

    def start(self):
        # calculates temperature
        self.temp0.set(b[0])

        # changes color of text to red if temp >= 50
        if b[0] >= 50:
            self.t0.config(fg="red")

Upvotes: 1

Views: 214

Answers (1)

user2555451
user2555451

Reputation:

Yes that works. You can use either:

self.t0.config(fg="red")

or:

self.t0["fg"] = "red"

Both methods do the same thing, so you can choose what you want.

Also, to get everything working, you will want to make this line of code:

self.t0 = Label(frame, fg="blue", textvariable=self.temp0,font=(20)).grid(row=2, column=0)

into two lines:

self.t0 = Label(frame, fg="blue", textvariable=self.temp0,font=(20))
self.t0.grid(row=2, column=0)

Now, self.t0 will point to the label like it should and not the return value of .grid, which is None.

Upvotes: 1

Related Questions