Change The Color Of A Tkinter Label Programmatically

Tkinter is a module in Python which is used to create GUI applications. It has many useful widgets such as Label, Button, Radiobutton, Checkbutton, Listbox, and more. In this article, we will learn about changing the color of a Tkinter label widget in Python.

Tkinter Label Widget

The Label is one of the widgets in Tkinter. It is used to display text in the GUI window. An image can also be displayed using the label widget. Also, a label can contain both text and image simultaneously in the window. The text in a Label can be formatted to change its font family, font size, foreground color, background color, font style, and more.

The syntax for creating a label in Tkinter is as follows:

label_name = tkinter.Label(parent,text="text_to_display")

Steps to Change the Color of a Tkinter Label Programmatically

Here is a step-by-step guide on how to change the color of a Tkinter Label programmatically.

Import Tkinter Module

First, you need to import the Tkinter module. In Python 3.x, the module is imported as tkinter.

import tkinter as tk

Create the Main Application Window

Next, create the main application window using tk.Tk() class. It is the parent window which will contain all other tkinter widgets. Here we provide a title and dimensions to the Tkinter window.

root = tk.Tk()
root.title("Read-Only Text Widget Example")
root.geometry("400x300")

Create the Label Widget

Next, create the Label widget by providing it a height and width. By default, the the color of the text of the label will be black.

label_wiget = tk.Label(parent, text="text_to_display")
text_widget.pack(padx=10, pady=10)

Change the Color of Lable

The color of the label can be changed programmatically by using the Tkinter config() function. It takes a parameter fg that is foreground color and assign it the color value you want to change the label into. You can either provide a string color name or the hexadecimal color code.

label_widget.config(fg="color_code")

Run the Main Event Loop

Finally, run the main event loop to display the window and make the application responsive to user interactions.

root.mainloop()

Code Implementation

Now let us see a few different examples to better understand how this works.

Example: In this example, we will create a label with some text and then change its color to ‘green’ when the ‘change color’ button is clicked by the user. Here we will provide the string color value to the foreground option of the config() function.

Python
# import tkinter module
import tkinter as tk

# Function to change the color of Label 
# on clicking the button 'removeImageButton'
def remove_image():
    # Color of Label assigned a color name
    label1.config(fg="green")

# Create the main application window
root = tk.Tk()
root.geometry("300x150")
root.title("Change Label Color Example")

# Label
label1 = tk.Label(root, text="Welcome to GFG",
                  font=("Times New Roman", 25, "bold"))
label1.pack(pady=20)

# Button to change the label color to green color.
removeImageButton = tk.Button(root, text="Change color", 
                              command=remove_image, 
                              bg="black", fg="white")
removeImageButton.pack()

# Run the main event loop
root.mainloop()

Output:

Changing Tkinter Label Color Programmatically


Example: In this example, we will create a label with some text and then change its color to ‘green’ when the ‘change color’ button is clicked by the user. Here we will provide the hexadecimal color code value to the foreground option of the config() function.

Python
# import tkinter module
import tkinter as tk

# Function to change the color of Label 
# on clicking the button 'removeImageButton'
def remove_image():
    # Color of Label assigned a hex code
    label1.config(fg="#0A7A16")

# Create the main application window
root = tk.Tk()
root.geometry("300x150")
root.title("Changing Label Color Example")

# Label
label1 = tk.Label(root, text="Welcome to GFG",
                  font=("Times New Roman", 25, "bold"))
label1.pack(pady=20)

# Button to change the label color to green color.
removeImageButton = tk.Button(root, text="Change color", 
                              command=remove_image, 
                              bg="black", fg="white")
removeImageButton.pack()

# Run the main event loop
root.mainloop()

Output:

Changing Tkinter Label Color Programmatically