简体   繁体   中英

Tkinter saying pyimage1 doesn't exist even though it does when trying to put it on a label

When running the code, I receive this error:

    [(<memory at 0x000001ADB67D2040>,)]
1
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=592x1052 at 0x1ADB67FF280>
=-----------------------------------=
pyimage1
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Player 1\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "c:\Users\Player 1\OneDrive\Documents\Year 13\Computer Science\NEA\Code\windows.py", line 373, in <lambda>
    images_button = tkinter.Button(self, text="View {} Images".format(listingname), font=('calibre', 20, 'bold'), command=lambda: self.create_command_for_Nav_Buttons("Pictures", condition=listing_id)).grid(column=0, row=0, rowspan=7)
  File "c:\Users\Player 1\OneDrive\Documents\Year 13\Computer Science\NEA\Code\windows.py", line 77, in create_command_for_Nav_Buttons
    start_window.create_page(condition)
  File "c:\Users\Player 1\OneDrive\Documents\Year 13\Computer Science\NEA\Code\windows.py", line 412, in create_page
    images = tkinter.Label(master=self, image=photo, width=300, height=300)
  File "C:\Users\Player 1\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3148, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\Player 1\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist

This is the part of the code that doesn't work.

class picture_page(Window):
    def __init__(self, current_window="Pictures!") -> None:
        super().__init__(current_window)
    
    def create_page(self, listing_id):
        imagesMView_list = dbm.getImagesForListing(listing_id)
        print(imagesMView_list)
        print(len(imagesMView_list))
        if len(imagesMView_list) == 0:
            self.create_Label("There are no images!", ('calibre', 20, 'bold'), 0, 0)
        else:

            processedImage_list = []
            for i in range(len(imagesMView_list[0])):
                processedImage_list.append(bytes(imagesMView_list[0][i]))


            # for i in range(len(processedImage_list)):
            #     img = Image.open(io.BytesIO(processedImage_list[i]))
            #     photo = ImageTk.PhotoImage(img)
            #     print(img)
            #     print(photo)
            #     tkinter.Label(master=self, image=photo).pack()
            for i in range(len(processedImage_list)):
                img = Image.open(io.BytesIO(processedImage_list[i]))
                print(img)
                print("=-----------------------------------=")
                photo = ImageTk.PhotoImage(img)
                print(photo)
                images = tkinter.Label(master=self, image=photo, width=300, height=300)
                images.grid(column=i, row=0)

The commented part that looks like code was another fix I tried. I also looked through these other stackoverflow questions ( Tkinter OOP "PyImage1" doesn't exist, error and Tkinter create image function error (pyimage1 does not exist) )

Thank you in advance!

EDIT: I have tried to put Global photo into my code to keep it as a reference, however that still causes the same issue to occur. I also wanted to say that this photo_page subclass inherits from the Windows Class and the Windows class inherits from Tk. This hasn't caused me any issues other than with putting an image in the label. The image comes from a database and in the database is stored as a byte Array and I convert it back into bytes and then into JPEG form (which I know is successful because I printed out the variables I assigned it to and they look fine).

The way I fixed my error was changing what my main class inherited. Initially, it was Windows(Tk): , however I changed it to Windows(Toplevel) and it fixed the issue.

have you by chance accidentally created two or more root windows? Objects in one root window cant see or interact with things in the second root window

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM