1

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

  • https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function?noredirect=1&lq=1 – Quack E. Duck Jun 20 '23 at 15:32
  • If adding (**outside** of your function, in the global scope) a list variable such as `picsList = []`, then inside your function doing `picsList.append(my_image)` fixes your problem, then this question will be a duplicate of the one linked above – Quack E. Duck Jun 20 '23 at 15:34
  • 2
    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 Jun 20 '23 at 16:18
  • Silly question, but have you imported an image library like Pillow? `from PIL import ImageTk, Image` – Zico Jun 20 '23 at 17:49

1 Answers1

1

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.