ttk.Label – Load JPG image from file

We can show a JPG image in a label widget by loading an image file.

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


myfile = r"/images/pizza.JPG"

root = tk.Tk()

with Image.open(myfile) as img:
     # Load the image into a format that Tk uses.
     tk_pizza = ImageTk.PhotoImage(img)

lbl_test = ttk.Label(root, image=tk_pizza)
lbl_test.pack()

root.mainloop()

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

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

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