ttk.Label – Load PNG image from file

The Label widget can be used for displaying an image.

img

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import tkinter as tk
from tkinter import ttk, PhotoImage


# Image file path
img_pizza = r"images/pizza.png"

# Load the image into a format that Tk can use.
tk_pizza = PhotoImage(file=img_pizza)

# Create and initialize the label
lbl_greeting = ttk.Label(root, 
                         text="Some Text.",
                         image=tk_pizza)

lbl_greeting.pack()

root.mainloop()

img

If we want to show the image and some text in the label, we need to use the compound setting.

lbl_greeting = ttk.Label(root, 
                         text="Some Text.",
                         image=tk_pizza,
                         compound=tk.LEFT)

img

Compound specifies where to show the image relative to the text.