How to change the text color using tkinter.Label

Viewed 55192

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

3 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.

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

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.

Related