-2

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).

  • Does this answer your question? [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) – Thingamabobs Aug 20 '22 at 15:29
  • Hi, thanks for your reply! However, when I tried the fixes from the link I still received the same error even though I made photo into a global variable or made photo into self.photo – Hassan Shafeeq Aug 20 '22 at 15:59
  • 1
    Do you create more than one instance of `Tk` or a class that inherits from `Tk`? This is a symptom of that. – Bryan Oakley Aug 20 '22 at 16:00
  • It might be possible because this subclass (photo_page) inherits a class called Windows which inherits Tk. Which looks like this; Class Windows(Tk): . However, this hasn't given me any issue with my other subclasses that are similar to photo_page other than with displaying my images in a Label and I know that the issue is not with my converting of byteA back to bytes. – Hassan Shafeeq Aug 20 '22 at 16:02
  • try to add `images.image = photo` right after you construct `images = tkinter.Label(..)`. If this does not help you need to show us a [mre] that we can ensure your architecture works as intended. – Thingamabobs Aug 20 '22 at 16:09
  • Hi, thanks for the reply! I did as you asked and made a minimal reproducible example and it seems that the issue is not in the code, rather I need to fix how I call each of my subclasses. If you have any general suggestions or could point me in the direction of how to format my OOP for tkinter, that would be amazing! Thank you for the help, it means a lot. – Hassan Shafeeq Aug 20 '22 at 16:54
  • Also, I would've given the code on this but it is hard to do so because my images are stored a MemoryViews in a database and it would be rude of me to ask for you to get your own MemoryView to byteArray and then JPEG and I think it would be risky to allow people to access my database. – Hassan Shafeeq Aug 20 '22 at 17:01
  • If you have more than one class that inherits from `Tk` or `Window`, that's the problem. Under most circumstances you should only ever have exactly one instance of `Tk`. – Bryan Oakley Aug 20 '22 at 23:45

2 Answers2

1

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.

-1

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

Katt
  • 1
  • It might be possible because this class inherits a class called Windows which inherits Tk. Which looks like this Class Windows(Tk): . However, this hasn't given me any issue other than with displaying my images in a Label and I know that the issue is not with my converting of byteA back to bytes. – Hassan Shafeeq Aug 20 '22 at 15:51
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 25 '22 at 05:33