How to Create Labels in Python with Tkinter?

In this tutorial, I will explain how to create labels in Python with Tkinter to display text and images in your GUI applications. As a developer based in the USA, I’ve encountered the need to create informative and visually appealing labels for various projects. Let’s get in and explore the capabilities of the Tkinter Label widget with practical examples.

Tkinter Label Widget

The Tkinter Label widget is a versatile tool that allows you to display text or images in your Python GUI applications. It serves as a read-only display box where you can convey important information to the user. Labels are essential for providing instructions, displaying results, or presenting any relevant data in your application.

To create a Label widget in Tkinter, you need to follow these basic steps:

  1. Import the Tkinter module
  2. Create the main application window
  3. Add the Label widget to the window
  4. Configure the Label properties as needed
  5. Pack or place the Label in the desired location

Here’s a simple example that demonstrates the creation of a Label:

import tkinter as tk

window = tk.Tk()
window.title("Employee Information")

label = tk.Label(window, text="Name: John Doe", font=("Arial", 12))
label.pack()

window.mainloop()

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter

In this example, we create a main window titled “Employee Information” and add a Label widget to display the name “John Doe” using the Arial font with a size of 12.

Read Python Tkinter search box

Configure Label Properties

The Tkinter Label widget offers various properties that you can configure to customize its appearance and behavior. Let’s explore some commonly used properties:

1. Text and Textvariable

The text property allows you to set the static text content of the Label. You can directly assign a string value to this property. For example:

label = tk.Label(window, text="Address: 123 Main St, New York")

I have executed the above example code and added the screenshot below.

How to Create Labels in Python with Tkinter

Alternatively, you can use the textvariable property to dynamically update the Label’s text based on the value of a Tkinter StringVar. This is useful when you need to display changing information. Here’s an example:

address = tk.StringVar()
label = tk.Label(window, textvariable=address)
address.set("Address: 123 Main St, New York")

Read Python Tkinter Validation examples

2. Font and Colors

You can customize the font style, size, and color of the Label text using the font and fg (foreground) properties. Here’s an example:

label = tk.Label(window, text="Phone: (123) 456-7890", font=("Helvetica", 14), fg="blue")

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter font and color

In this case, the Label will display the phone number using the Helvetica font with a size of 14 and a blue color.

Read Python Tkinter On-Off Switch

3. Background Color

To set the background color of the Label, you can use the bg property. For example:

label = tk.Label(window, text="Email: john.doe@example.com", bg="pink")

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter Background Color

This Label will have a light gray background color.

Read Python Tkinter On-Off Switch

4. Padding and Borders

  • There are 6 types of borders:
    • Flat
    • Raised
    • Sunken
    • ridge
    • solid
    • groove
  • By default flat is active.
  • borderwidth keyword is used to define the thickness of the border.
  • relief keyword is used to define the type of border.

Let us see an example.

from tkinter import *

# Create the main window
ws = Tk()
ws.title("Border Styles Example")
ws.geometry("300x400")

# Add labels with different border styles
Label(ws, text="Flat Border", borderwidth=3, relief="flat", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="Raised Border", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="Sunken Border", borderwidth=3, relief="sunken", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="Ridge Border", borderwidth=3, relief="ridge", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="Solid Border", borderwidth=3, relief="solid", padx=5, pady=10).pack(padx=5, pady=10)
Label(ws, text="Groove Border", borderwidth=3, relief="groove", padx=5, pady=10).pack(padx=5, pady=10)

# Run the main event loop
ws.mainloop()

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter Borders

This example shows the different styles of borders.

You can add padding to the Label using the padx, pady, bd

Here’s an example:

label = tk.Label(window, text="Job Title: Software Engineer", padx=10, pady=5, bd=2, relief=tk.SOLID)

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter Padding and Borders

In this case, the Label will have a padding of 10 pixels on the left and right, 5 pixels on the top and bottom, a border width of 2 pixels, and a solid border style.

Read Python Tkinter notebook Widget

Update Label Text Dynamically

In many scenarios, you may need to update the text of a Label dynamically based on user actions or other events. You can achieve this by using the config() method or by modifying the associated StringVar.

Here’s an example that updates the Label text when a button is clicked:

def update_label():
    name = "Sarah Johnson"
    label.config(text="Name: " + name)

button = tk.Button(window, text="Update", command=update_label)
button.pack()

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter update lable

In this case, when the “Update” button is clicked, the update_label() function is called, and it updates the Label’s text to display a new name.

Read Python Tkinter Events

Organize Labels in a Layout

When creating multiple Labels in your application, it’s important to organize them in a logical layout. Tkinter provides several layout managers, such as pack(), grid(), and place(), to arrange widgets in a window.

Here’s an example that demonstrates using the grid() layout manager to arrange Labels in a table-like structure:

name_label = tk.Label(window, text="Name:")
name_label.grid(row=0, column=0, padx=5, pady=5)

name_value = tk.Label(window, text="John Doe")
name_value.grid(row=0, column=1, padx=5, pady=5)

email_label = tk.Label(window, text="Email:")
email_label.grid(row=1, column=0, padx=5, pady=5)

email_value = tk.Label(window, text="john.doe@example.com")
email_value.grid(row=1, column=1, padx=5, pady=5)

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter grid

This code creates a grid-like layout with labels for name and email along with their corresponding values.

Here’s an example that demonstrates using the place() layout manager to arrange Labels in a table-like structure:

import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("Place Example")
window.geometry("300x150")

# Name label and value
name_label = tk.Label(window, text="Name:")
name_label.place(x=10, y=10)

name_value = tk.Label(window, text="John Doe")
name_value.place(x=100, y=10)

# Email label and value
email_label = tk.Label(window, text="Email:")
email_label.place(x=10, y=40)

email_value = tk.Label(window, text="john.doe@example.com")
email_value.place(x=100, y=40)

# Run the main event loop
window.mainloop()

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter place

This code uses the place() geometry manager to position labels with their corresponding values at specific (x, y) coordinates. It provides precise control over widget placement, allowing for customized layouts. This approach is ideal when you need exact positioning for your widgets in the window.

Read Python Tkinter Animation

Tkinter label text-alignment

  • Alignment refers to the quick position of text direction-wise.
  • direction-wise means:
    • N: North
    • S: South
    • E: East
    • W: West
    • NE: North East
    • NW: North West
    • SE: South East
    • SW: South West
    • CENTER
  • anchor keyword is used to position the label widget.

Syntax: In place of direction provide anyone out of these (SNSEWNESESWCENTER)

Label(ws, text="sometext", anchor= direction).pack()
from tkinter import *

ws = Tk()
ws.title("Border")
ws.geometry("50x100")

Label(ws, text="Left", anchor=W).pack(fill='both')
Label(ws, text="Right", anchor=E).pack(fill='both')

ws.mainloop()

I have executed the above example code and added the screenshot below.

Create Labels in Python with Tkinter Borders text align

In this output, you can see the text has been aligned to left and right using anchor.

Read Python Tkinter Multiple Windows Tutorial

Create a Transparent Label

To create a label with a transparent background in Tkinter, you can use the Label widget in combination with the wm_attributes() method. Here’s an example that demonstrates how to achieve this:

import tkinter as tk

window = tk.Tk()
window.title("Transparent Label Example")

# Create a canvas with an image
canvas = tk.Canvas(window, width=400, height=400)
canvas.pack()

image = tk.PhotoImage(file="background.png")
canvas.create_image(0, 0, anchor=tk.NW, image=image)

# Create a transparent label
label = tk.Label(window, text="Transparent Label", font=("Arial", 16), bg="white")
label.place(x=100, y=100)

# Make the label background transparent
label.wm_attributes("-transparentcolor", "white")

window.mainloop()

By setting the “-transparent color” attribute to the same color as the label’s background color, Tkinter will treat that color as transparent, allowing the underlying image or background to be visible through the label.

Read Python Tkinter Editor + Examples

Customize the Transparent Label

You can further customize the transparent label by modifying its properties such as the text, font, and position. Here are a few examples:

  • Changing the label text:
  label.config(text="New Transparent Label")
  • Modifying the font style and size:
  label.config(font=("Helvetica", 12, "bold"))
  • Adjusting the label position:
  label.place(x=200, y=200)

Feel free to experiment with different configurations to achieve the desired appearance for your transparent label.

Read Python Tkinter Table Tutorial

Use Images in Labels

In addition to text, Tkinter Labels can also display images. To use an image in a Label, you need to follow these steps:

  1. Create a PhotoImage object from an image file
  2. Configure the Label’s image property with the PhotoImage object

Here’s an example that demonstrates displaying an image on a Label:

image = tk.PhotoImage(file="employee.png")
label = tk.Label(window, image=image)
label.pack()

Make sure to provide the correct file path for the image.

Read Python Tkinter Quiz – Complete tutorial

Conclusion

In this tutorial, I have explained how to create labels in Python with Tkinter. I discussed how to configure label properties , update label text dynamically, and organize labels in a layout. I also discussed how to align text, create a transparent label , customize the transparent label, and use an image in a label.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.