Tkinter Label

Summary: in this tutorial, you’ll learn about the Tkinter Label widget and how to use it to display a text or image on the screen.

Introduction to Tkinter Label widget #

Tkinter Label widget displays a text or image on a window. To create a Label widget, you follow these steps:

First, import ttk module from tkinter:

from tkinter import ttkCode language: JavaScript (javascript)

Second, create a Label widget using the Label constructor:

label = ttk.Label(master, **kw)Code language: Python (python)

The Label widget has many options that allow you to customize its appearance.

Displaying a text label #

The following program shows how to display a text label on the main window:

import tkinter as tk
from tkinter import ttk

# Main window
root = tk.Tk()
root.geometry('300x200')
root.title('Label Widget Demo')

# Create a label
label = ttk.Label(root, text='This is a label')
label.pack()

root.mainloop()Code language: Python (python)

Output:

How it works.

First, create a new instance of the Label widget:

label = ttk.Label(root, text='This is a label')Code language: JavaScript (javascript)

Second, place the Label on the main window by calling the pack() method:

label.pack()Code language: CSS (css)

If you don’t call the pack() function, the program still creates the label but does not show it on the main window.

The pack() function is one of three geometry managers in Tkinter, including:

You’ll learn about this in detail in the upcoming tutorials.

Setting fonts for the Label widget #

To set a font for a label, you pass the font keyword argument to the Label constructor like this:

font = ('font name', font_size)Code language: Python (python)

The font keyword argument is a tuple that contains font name and size. For example:

font=("Helvetica", 14)Code language: Python (python)

The following program creates a label with the Helvetica font and display it on the main window:

import tkinter as tk
from tkinter import ttk


root = tk.Tk()
root.geometry('300x200')
root.title('Label Widget Demo')

label = ttk.Label(
    root,
    text='A Label with the Helvetica font',
    font=("Helvetica", 14))

label.pack()

root.mainloop()Code language: Python (python)

Output:

Creating image labels #

To display an image label, you follow these steps:

First, create a directory called assets, download and copy the following python.png file to the assets directory:

Second, create a PhotoImage widget by passing the path of the image to the PhotoImage constructor:

photo = tk.PhotoImage(file='./assets/python.png')Code language: Python (python)

Third, assign the PhotoImage object to the image option of the Label widget:

Label(..., image=photo)Code language: Python (python)

The following example shows how to use a Label widget to display an image:

import tkinter as tk
from tkinter import ttk

# create the main window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Label Widget Image')

# display an image label
photo = tk.PhotoImage(file='./assets/python.png')
image_label = ttk.Label(
    root,
    image=photo,
    padding=5
)
image_label.pack()

root.mainloop()Code language: Python (python)

Output:

Note that the image file is located at the /assets/ folder.

To display both text and image, you’ll use the text attribute and compound option.

The compound option specifies the position of the image relative to the text. Its valid values are:

CompoundEffect
'top'Display the image above the text.
'bottom'Display the image below the text.
'left'Display the image to the left of the text.
'right'Display the image to the right of the text.
'none'Display the image if there’s one; otherwise, display the text. The compound option defaults to 'none'.
'text'Display the text, not the image
'image'Display the image, not the text.

The following program shows how to display both text and image on a label:

import tkinter as tk
from tkinter import ttk

# create the root window
root = tk.Tk()
root.geometry('300x200')
root.title('Label Widget Image')

# display an image label
photo = tk.PhotoImage(file='./assets/python.png')
image_label = ttk.Label(
    root,
    image=photo,
    text='Python',
    compound=tk.TOP
)
image_label.pack()

root.mainloop()Code language: Python (python)

Output:

Summary #

  • Use the Tkinter Label widget to display text or images.
Was this tutorial helpful ?