Registration of the image on the tkinter label continues to result in an error that the image does not exist

  Kiến thức lập trình

Before we talk, I’m not good at English, so I’m using a translator to ask questions. Please excuse me for awkward sentences.

from tkinter import *
from PIL import Image, ImageTk
import os
import random


class ImageWindow:
    def __init__(self, title):
        self.window = Tk()
        self.window.title(title)
        self.image = Image.open('cat6.jpg')
        self.image = ImageTk.PhotoImage(self.image)
        self.label = Label(self.window, image=self.image)
        self.label.pack()

This class is configured to display a label window containing images when called.

self.label = Label(self.window, image=self.image)

However, the following error occurs when the above code is executed.

self.tk.call( _tkinter.TclError: image "pyimage202" doesn't exist

To resolve this error, I also checked the path of the image file and checked that the image was fine, so there was no problem with this. Also, I created a label containing images in another class in a similar way as above, but there was no problem at that time, so I don’t know why.

It’s the first time I’ve ever asked a question like this, so I’m really sorry if there’s any problem.
And I would really appreciate it if you could tell me how to solve it.

New contributor

user24545957 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT