Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the text color using tkinter.Label

I'm trying to build my first GUI program and want to know who to change the label text color? for instance, changing it to 'red'

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="what's my favorite video?", pady=10, padx=10, font=10,)
label.pack()
click_here = tk.Button(root, text="click here to find out", padx = 10, pady = 5)
click_here.pack()

root.mainloop()

Thanks a lot :-)

like image 526
T.Stimer Avatar asked Oct 10 '20 04:10

T.Stimer


People also ask

How do you change the color of a text button in Python?

We can also change the foreground color by using the 'fg' property of the button widget in Tkinter. We can also use the configure method to change the color. As we know that we can use the 'bg' and 'fg' property of the Button widget to change the color, we need to pass this variable while using the Button object.

Can you update labels in tkinter?

This is the easiest one , Just define a Function and then a Tkinter Label & Button . Pressing the Button changes the text in the label.


Video Answer


2 Answers

You can use the optional arguments bg and fg (Note that you might need to use a different option like highlightbackground on MacOS system as stated In this answer ) - which I believe is a known issue with tk.Button on MacOS.

import tkinter as tk

root = tk.Tk()

# bg is to change background, fg is to change foreground (technically the text color)
label = tk.Label(root, text="what's my favorite video?",
                 bg='#fff', fg='#f00', pady=10, padx=10, font=10) # You can use use color names instead of color codes.
label.pack()
click_here = tk.Button(root, text="click here to find out",
                       bg='#000', fg='#ff0', padx = 10, pady = 5)
click_here.pack()

root.mainloop()

The only reason I added this as an answer is because the last answer I wrote on a similar question for someone on SO, didn't work just because they were using a Mac. If you are on a Windows machine, you are fine.

like image 134
P S Solanki Avatar answered Nov 18 '22 03:11

P S Solanki


You can use bg='#fff' or fg='f00' in tk.label.

like image 32
Tahil Bansal Avatar answered Nov 18 '22 03:11

Tahil Bansal