How To Change The Text Color Using Tkinter.Label

In Tkinter, we can customize the styling of elements such as labels to enhance the appearance of our GUI applications. One common customization is changing the text color of a Label widget. In this article, we will explore different approaches to changing the text color using tkinter.label in Python.

Change The Text Color Using Tkinter.Label

Below are the possible approaches to changing the text color using tkinter.label in Python:

  • Using the fg (Foreground) Parameter
  • Using the config() Method
  • Using a Custom Style with ttk

Change The Text Color Using the fg (Foreground) Parameter

In this example, we are using the fg (foreground) parameter of the Label widget to change the text color to red. Additionally, the font parameter is used to set the font to “Helvetica” with a size of 16, making the text larger and red.

Python
import tkinter as tk

root = tk.Tk()
root.title("Change Text Color - Approach 1")

label = tk.Label(root, text="Hello w3wiki", fg="red", font=("Helvetica", 16))
label.pack(pady=20)

root.mainloop()

Output:

Change The Text Color Using the config() Method

In this example, we are using the config() method to change the text color of the Label widget to green. The label is initially created with a font size of 16 using the font parameter, and then the config() method is called to set the fg (foreground) parameter to green.

Python
import tkinter as tk

root = tk.Tk()
root.title("Change Text Color - Approach 2")

label = tk.Label(root, text="Hello w3wiki", font=("Helvetica", 16))
label.config(fg="green")
label.pack(pady=20)

root.mainloop()

Output:

Change The Text Color Using a Custom Style with ttk

In this example, we are using the ttk.Style class to create a custom style that sets the text color to blue. The style is configured with the foreground parameter set to blue and the font parameter set to “Helvetica” with a size of 16.

Python
import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title("Change Text Color - Approach 3")

style = ttk.Style()
style.configure("TLabel", foreground="blue", font=("Helvetica", 16))

label = ttk.Label(root, text="Hello w3wiki", style="TLabel")
label.pack(pady=20)

root.mainloop()

Output: