Fixing Python and tkinter error – _tkinter.TclError: image “…” doesn’t exist

Python and tkinter error – _tkinter.TclError: image “…” doesn’t exist is an error which occurs when you do not correctly handle images in tkinter.

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

Exploring the Python and tkinter error – _tkinter.TclError: image “…” doesn’t exist

This is an error which occurs when you do not correctly handle images in tkinter.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
_tkinter.TclError: image "< yourimagehere mode=RGBA size=AxB at >" doesn't exist
                                                                       #

Check out the sections bellow for a number of tested solutions that I have tried and that have worked for me.

The method I used to solve this problem : Correctly use Image and ImageTk in Tkinter

We have already established that you get this error when you do not handle images in tkinter the way you should be handling them.

This is going to be a step by step method so please follow along.

When working with tkinter, the first thing you should start with in our particular case is this

                                                                       #
from Tkinter import *
from PIL import Image, ImageTk
import os
                                                                       #

The error occurs when you do not use the PhotoImage instance as your image value. Just like this

                                                                       #
window = Tk()
x = Image.open(Imagepath) 
if os.path.isfile(Imagepath):
                                                                       #

You should do the opposite and use it as image value just like this

                                                                       #
img = Image.open(Imagepath)
photo = ImageTk.PhotoImage(img)
var = var(window, image=photo)
                                                                       #

The final thing you should add to your code is this

                                                                       #
var.image=phphoto
                                                                       #

The goal here is to keep a reference of your image. Thank you for reading this post, I think the error should be gone by now. Cheers.

Summing-up : 

This is the end of this article, I hope we helped you solve the error : Python and tkinter error _tkinter.TclError image doesn’t exist or at least show you why it happens and guided you in the right direction. If you want to help, you can donate to our Kofi account using the red button at the top of this page.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/