Tkinter Designer

← tkinter how-to

Why is my tkinter image not showing?

If the label appears as an empty space the size of the picture and there is no error, Python has garbage-collected the image. A PhotoImage lives only as long as some Python variable refers to it. Created inside a function or method and stored nowhere else, it is deleted when the function returns, and the label goes blank. Keep a reference that lasts as long as the window:

import tkinter as tk

class App:
    def __init__(self, root):
        # Stored on self, so it lives as long as the window does
        self.logo = tk.PhotoImage(file="logo.png")
        tk.Label(root, image=self.logo).pack()

root = tk.Tk()
app = App(root)
root.mainloop()

Where the reference gets lost

tkinter does not hold on to the image for you. Passing it as image= gives the label only the image's name, pyimage1, so the label keeps its space and draws nothing once the Python object is gone. Three places this happens: a local variable in a function, an image swapped in from a button handler, and a loop that reuses one variable, where only the last picture survives.

For a single widget, the shortest fix is to store the image on the widget itself. For many images, keep them in a list.

def show_next():
    new = tk.PhotoImage(file="next.png")
    label.configure(image=new)
    label.image = new        # without this, the label goes blank

images = []
for name in ["a.png", "b.png", "c.png"]:
    img = tk.PhotoImage(file=name)
    images.append(img)       # without this, only c.png shows
    tk.Label(root, image=img).pack()

couldn't recognize data in image file "photo.jpg"

Tk 8.6, which almost every Python ships with, reads PNG, GIF and PPM/PGM by itself, but not JPEG. Open other formats with Pillow (pip install pillow) and convert with ImageTk.PhotoImage. The reference rule is the same: a Pillow image nothing refers to goes blank too.

from PIL import Image, ImageTk

self.photo = ImageTk.PhotoImage(Image.open("photo.jpg"))
tk.Label(root, image=self.photo).pack()

couldn't open "logo.png": no such file or directory

A bare filename is looked up in the folder Python was started from, not the folder your script is in. Running python main.py from inside the project works, while VS Code's Run button, an IDE, or a shortcut that starts somewhere else fails with this error even though the file is right next to the script. Build the path from the script's own location instead.

from pathlib import Path

HERE = Path(__file__).resolve().parent
self.logo = tk.PhotoImage(file=HERE / "logo.png")

image "pyimage1" doesn't exist

This means the image belongs to a different Tk() from the widget showing it. Each tk.Tk() is a separate Tcl interpreter, and an image created in the first one cannot be shown in the second. Usually the cause is a second window made with tk.Tk(). Make extra windows with tk.Toplevel() instead, and the same image works in all of them.

RuntimeError: Too early to create image

Images are created inside Tk, so the window has to exist first. Both tk.PhotoImage and Pillow's ImageTk.PhotoImage raise Too early to create image: no default root window when they run before tk.Tk(), which typically happens with images loaded at the top of a module. Move the loading to after the root is created.

Resizing the image

PhotoImage can only scale by whole numbers: zoom(2) doubles it and subsample(2) halves it. For any other size, resize with Pillow before converting.

self.photo = ImageTk.PhotoImage(Image.open(HERE / "photo.jpg").resize((200, 150)))

In CustomTkinter

CustomTkinter widgets take a CTkImage, which handles scaling on high-DPI screens. Pass text="" to the label, or it shows the default text "CTkLabel" beside the picture. A CTkImage will not draw on a canvas, though, which has its own page.

import customtkinter as ctk
from PIL import Image

self.logo = ctk.CTkImage(Image.open(HERE / "logo.png"), size=(120, 80))
ctk.CTkLabel(root, image=self.logo, text="").pack()

Doing it without the lookup

The Image widget writes this for you. The export stores each image on self with a comment saying why, opens it with Pillow when the format or a resize needs it, and scales it to the box you drew unless you switch that off. It also opens the file from the script's own folder rather than wherever Python was started, so VS Code's Run button works. Set the file name in the inspector and put the file next to the exported script.

This example is ready to open: change it on the canvas and watch the Python update.

Open this example in the designer →

Related questions