pythonfiletkintercanvasphotoimage

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


Can't find any not outdated answers, I'm creating a canvas and running a filepath through PhotoImage so I can create_image in the canvas, but I'm getting an error that says the image {path} does not exist, despite the image obviously existing because I used the exact same path to get it with requests, and I can open the file and there's nothing wrong with the path or the image. in init:

self.cover_canvas = Canvas(width=100, height=200)

later in the class:

def load_image(self):
    self.cover_image = PhotoImage(self.image_loc)
    self.cover_canvas.create_image(50, 100, image=self.cover_image)
    self.cover_canvas.grid(row=4, column=1)

Edit: The PIL solution ended up working, thought it might be outdated because when I tried to pip install PIL it failed to find a current version, but I just tried PIP installing is with the name "Pillow" and at first I thought it still didn't work because when I tried to import from Pillow nothing worked, but when I tried importing from PIL after pip installing Pillow it worked. Kinda weird but wtv.


Solution

  • although I usually prefer to use a Label to show images, below I show you a possible solution using a Canvas. The program loads. png files and resizes them, I hope this can help you.

        #!/usr/bin/python3
    import os
    import tkinter as tk
    from tkinter import ttk
    from tkinter import messagebox
    from tkinter import filedialog as fd
    from PIL import Image, ImageTk
    
    class App(tk.Tk):
        """Start here"""
    
        def __init__(self):
            super().__init__()
    
            self.title("Hello World!")
            self.protocol("WM_DELETE_WINDOW", self.on_exit)
            self.init_ui()
    
        def init_ui(self):
    
            f0 = tk.Frame(self, relief=tk.GROOVE, padx=8, pady=8)
            f1 = tk.Frame(f0, relief=tk.GROOVE, padx=8, pady=8)
    
            self.canvas = tk.Canvas(f1, relief=tk.GROOVE, borderwidth=1)
            self.canvas.pack(fill=tk.BOTH, expand=1)
            
            w = tk.Frame(f0, relief=tk.GROOVE, borderwidth=1, padx=8, pady=8)
            ttk.Button(w, text="Load", command=self.on_get_image).pack()
            ttk.Button(w, text="Close", command=self.on_exit).pack()
                    
            f0.pack(fill=tk.BOTH, expand=1)
            f1.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
            w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
    
        def on_get_image(self, evt=None):
    
            selected_file = fd.askopenfilename(filetypes=(("Choice files", "*.png"),), parent=self)
    
            if selected_file:
                i = self.on_set_image(selected_file)
                #image is positioned relative to point (x, y).
                self.canvas.create_image(1, 1, image=i, anchor=tk.NW)
                self.canvas.image = i
            
        def on_set_image(self, filename):
    
            image = Image.open(filename)
            # if you resize...
            image= image.resize((800,600), Image.ANTIALIAS)
            return ImageTk.PhotoImage(image)
           
        def on_exit(self):
            """Close all"""
            if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
                self.destroy()               
        
    if __name__ == '__main__':
        app = App()
        app.mainloop()
    

    enter image description here