Boxeez
Posts: 6
Joined: Wed Jan 13, 2016 9:32 pm

Is there a way to change label fg colour with Tk?

Thu Jan 21, 2016 4:05 pm

Hi
I'd like to change the foreground of a label, when a int reaches a defined value (Button or something like this)
How does that work?
My label looks like this:

Code: Select all

self.Label = tk.Label(self, textvariable = self.LabelText[0][0], fg=colour.get(), bg="white")
and then I try to:

Code: Select all

colour.set("red")
That works, when I set the colour before creating the label, but I need it after that.

Thanks a lot :)

DirkS
Posts: 9902
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Is there a way to change label fg colour?

Thu Jan 21, 2016 4:33 pm

Please please please... if you're posting a problem, give at least some details about your setup.
From that bit of code I gather you're using tkinter with python.
Last edited by DirkS on Thu Jan 21, 2016 9:15 pm, edited 1 time in total.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12124
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Is there a way to change label fg colour?

Thu Jan 21, 2016 7:10 pm

moved to python part of forum.

Boxeez
Posts: 6
Joined: Wed Jan 13, 2016 9:32 pm

Re: Is there a way to change label fg colour?

Thu Jan 21, 2016 7:24 pm

Thank you guys for response, so sorry for that.

This is my code:

Code: Select all

from tkinter import *

CounterValue = 0

class Application(Frame):

    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()

        self.counter = IntVar()
        self.colour = StringVar()
        
        self.counter.set(0)
        self.colour.set("black")

        sButton = Button(self, border=1, text="BUTTON",command=lambda: self.CountUp())
        sButton.pack()
        sLabel = Label(self, textvariable = self.counter, fg=self.colour.get(), bg="white")
        sLabel.pack()

    def CountUp(self):
        global CounterValue
        CounterValue = CounterValue +1
        print(CounterValue)
        self.counter.set(CounterValue)
        if(CounterValue > 10):
            self.colour.set("red")
    



root = Tk()
app = Application(root)
root.mainloop() 

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Is there a way to change label fg colour?

Fri Jan 22, 2016 9:47 am

You need to call the cofigure method of the button.
Try this:

Code: Select all

from tkinter import *

CounterValue = 0

class Application(Frame):

    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()

        self.counter = IntVar()
        self.colour = StringVar()
       
        self.counter.set(0)
        self.colour.set("black")
        
        #sButton = Button(self, border=1, text="BUTTON",command=lambda: self.CountUp())
        #sButton.pack()
        self.sButton = Button(self, border=1, text="BUTTON",command=lambda: self.CountUp())
        self.sButton.pack()
        
        sLabel = Label(self, textvariable = self.counter, fg=self.colour.get(), bg="white")
        sLabel.pack()

    def CountUp(self):
        global CounterValue
        CounterValue = CounterValue +1
        print(CounterValue)
        self.counter.set(CounterValue)
        if(CounterValue > 10):
            #self.colour.set("red")
            self.sButton.configure(bg='red')
   
root = Tk()
app = Application(root)
root.mainloop()

Boxeez
Posts: 6
Joined: Wed Jan 13, 2016 9:32 pm

Re: Is there a way to change label fg colour?

Fri Jan 22, 2016 6:35 pm

Thank you but I'd like to change the colour from the label (from the value how may times the button was pressed). On this way will the backgroundcolour from the button change. (and not from the label under the button)

Thanks

User avatar
RogerW
Posts: 285
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Is there a way to change label fg colour with Tk?

Fri Jan 22, 2016 6:47 pm

You can use the configure fuction on the label instead of the button.

hippy
Posts: 5782
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is there a way to change label fg colour with Tk?

Fri Jan 22, 2016 7:04 pm

I think what you are trying to do is have a variable which, when you change the colour name it holds, the label also changes colour much like you can do with the label text. That is achieved by tying the variable to the label using "textvariable=", but unfortunately there is no equivalent for doing that for foreground or background colour. You have to issue a .configure to change the colour.

User avatar
DougieLawson
Posts: 35805
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Is there a way to change label fg colour with Tk?

Sat Jan 23, 2016 8:03 am

Code: Select all

label.config(fg ='green')
should work. Obviously you'll need to change that to reference the right label object.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

andyj280
Posts: 3
Joined: Fri Aug 16, 2019 3:39 am

Re: Is there a way to change label fg colour with Tk?

Fri Aug 16, 2019 4:01 am

DougieLawson wrote:
Sat Jan 23, 2016 8:03 am

Code: Select all

label.config(fg ='green')
should work. Obviously you'll need to change that to reference the right label object.
I am trying this but it is not working for me :(. I tried calling label.update() afterwards, but that didn't work.
It might be worth mentioning that I'm calling this code from within a method/function.

Return to “Python”