0

I am trying to display an image in a GUI, and don't understand what is wrong. I keep getting this error:

AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

What should my code (especially the line with my_img=...) look like?

My Code:

from tkinter import *
from PIL import ImageTk,Image

my_img = ImageTk.PhotoImage(Image.open("iu.jpeg"))
my_label = Label(image=my_img)
my_label.pack()


root = Tk()
root.title("ICON PRACTICE")
root.iconbitmap('iu.ico')

button_quit = Button(root, text = "EXIT", command=root.quit)
button_quit.pack()
root.mainloop()

The full error

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    my_img = ImageTk.PhotoImage(Image.open("test.png"))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 112, in __init__
    self.__photo = tkinter.PhotoImage(**kw)
  File "/usr/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.8/tkinter/__init__.py", line 3997, in __init__
    raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x7f7148fadc10>
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

Xbox One
  • 307
  • 2
  • 13
TGercken
  • 79
  • 9
  • You should put the three lines (after import statements) after creating `root` instance, i.e. before `button_quit = ...`. – acw1668 Mar 16 '20 at 04:23
  • 1
    Have you tried moving the creation of the root window before you tried creating the image? Also, please tell us what line created the error. We can guess, but it's better if we don't have to. – Bryan Oakley Mar 16 '20 at 04:35
  • Does this answer your question? [What is a parent/master that initialize a tkinter class?](https://stackoverflow.com/a/59008955/7414759) – stovfl May 29 '20 at 20:04

2 Answers2

0

Try doing this, its probably because you create the root object after opening the image and creating the photo object.

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

root= Tk()
i = Image.open("C:/path/to/the/image/directory/image.png") 
photo = ImageTk.PhotoImage(i)

root.mainloop()
JJerome
  • 64
  • 8
  • 2
    Why not just pass the full pathname of the image to `Image.open()`? Changing current directory to the image directory may affect the other part of the program. – acw1668 Mar 16 '20 at 04:03
  • 1
    Also the import statements: `import Image`, `import ImageTk` and `from PIL import Tk` are incorrect. – acw1668 Mar 16 '20 at 04:18
  • 1
    If the path was the problem, the OP would have gotten a different error. – Bryan Oakley Mar 16 '20 at 04:34
0

The Label widget will only work after root = Tk() (Tk() starts the underlying Tcl interpreter) is declared. Then, all child widgets must have root as their first parameter (e.g., Label(root, text='hi')). You started the interpreter after you tried to use it, so Python raised an exception.

Xbox One
  • 307
  • 2
  • 13