Search code examples
pythoncustomtkinter

Why I'm getting this error when displaying an image in customtkinter?


I'm trying to display an image from a specific folder, but the error "_tkinter.TclError: image "pyimage2" doesn't exist" is showing. I have verified the image path, and it's correct.

This is a project where many nested functions are called. I suppose this may be the cause of the error, since I have tried this isolated function in another app and it's working properly.

def display_image(csv_file_name, df, frame):
    # Create image path
    image_path = current_folder_path + '/' + csv_file_name + '.png'
    # Create image widget
    my_image = customtkinter.CTkImage(Image.open(image_path), size=(25, 25))
    # Create label to display image
    image_label = customtkinter.CTkLabel(frame, text="", image=my_image)
    image_label.pack()

_tkinter.TclError: image "pyimage2" doesn't exist


Solution

  • Possibly you've created more than one instance of Tk()/CTk(). Each image object is tied to one instance, and won't work with widgets that are part of a different instance. If this is the problem, use Toplevel() (or whatever the customtkinter equivalent is) to create additional windows. – jasonharper

    *This was the reason of the error. I tried to use a class designed for displaying toplevel windows in customtkinter and it worked. Thanks to jasonharper.