Tkinter Label Widget

Tkinter Label Widget

In this tutorial, you’ll learn about the Tkinter Label widget and how to use it to display text or images on the screen.

Introduction to Tkinter Label Widget

The Tkinter Label widget is used to display text or images on the screen. The general syntax for creating a Label widget is:

label = ttk.Label(master, **options)
Code copied!

Label Options

The Label widget has various options to customize its appearance:

OptionMeaning
anchorPosition of text/image when smaller than the width (e.g., tk.W, tk.CENTER, tk.E)
backgroundSet the background color for the label
borderwidthCreate effects such as flat, raised, sunken, etc.
fontSpecify the font style for displaying text
imageSpecify an image to show in addition to or instead of text
compoundHow to display both text and image

Displaying a Regular Label

The following program shows how to display a regular label on the root 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='This is a label')
label.pack(ipadx=10, ipady=10)

root.mainloop()
Code copied!

Setting a Specific Font for the Label

To set a particular font for a label, pass the font keyword argument to the Label constructor:

font = ('font name', font_size)  # Example: font=("Helvetica", 14)
Code copied!

The following example shows a label with the Helvetica font:

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(ipadx=10, ipady=10)

root.mainloop()
Code copied!

Displaying an Image

To use a Label widget to display an image, follow these steps:

  1. Create a PhotoImage widget:
  2. photo = tk.PhotoImage(file='./path_to_image/image.png')
    Code copied!
  3. Assign the PhotoImage object to the image option of the Label widget:
  4. image_label = ttk.Label(root, image=photo)
    Code copied!

Here's how to display an image using a Label widget:

import tkinter as tk
from tkinter import ttk

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

photo = tk.PhotoImage(file='./path_to_image/image.png')
image_label = ttk.Label(root, image=photo, padding=5)
image_label.pack()

root.mainloop()
Code copied!

Displaying Both Text and Image

To display both text and image, use the text attribute and the compound option:

image_label = ttk.Label(root, image=photo, text='Image Title', compound='top')
Code copied!

Here's an example that combines both:

import tkinter as tk
from tkinter import ttk

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

photo = tk.PhotoImage(file='./path_to_image/image.png')
image_label = ttk.Label(root, image=photo, text='Image Title', compound='top')
image_label.pack()

root.mainloop()
Code copied!

Conclusion

In this tutorial, you learned how to use the Tkinter Label widget to display text or images effectively. The Label widget provides various options for customization, making it a flexible tool for your GUI applications.