How to change the text color using tkinter.Label
Asked Answered
L

5

26

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 :-)

Leadin answered 10/10, 2020 at 4:58 Comment(2)
Add fg='red' in tk.Label(...).Sensible
try bg='#fff' or fg='f00' in tk.labelAgglutination
H
19

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.

Hilar answered 10/10, 2020 at 7:44 Comment(2)
@JackGriffin i'm not sure what that means. #f00 will always represent red regardless of its host OS.Hilar
@P S Solanki : you're right.I misunderstood your code.My fault.Will delete that comment.Disenthral
A
4

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

Assertion answered 11/2, 2021 at 21:36 Comment(0)
C
0

note that background='' returns a label to its default color. I found this by printing the result of label.cget('background') where label is a tkinter label.

Career answered 20/12, 2021 at 19:2 Comment(1)
Does not work on Linux (Kubuntu 20.04)Disenthral
H
0
import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="what's my favorite video?", pady=10, padx=10, font=10, fg="red")
label.pack()

click_here = tk.Button(root, text="click here to find out", padx=10, pady=5)
click_here.pack()

root.mainloop()
Hotspur answered 17/10, 2023 at 17:33 Comment(0)
U
0
label = tk.Label(root, text="what's my favorite video?", pady=10, padx=10, font=10, text_color="red")

use the argument 'text_color' to chnage your label text's colour

Unwitnessed answered 18/11, 2023 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.