Ошибка AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' в tkinter
Ошибка:
Traceback (most recent call last):
File "C:\Users\Asus\Desktop\program\rubik\main.py", line 5, in <module>
im=ImageTk.PhotoImage(file="C:/Users/Asus/Desktop/program/rubik/cube.png")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Asus\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\ImageTk.py", line 126, in __init__
self.__photo = tkinter.PhotoImage(**kw)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Asus\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 4130, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Asus\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 4063, in __init__
master = _get_default_root('create image')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Asus\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 319, in _get_default_root
raise RuntimeError(f"Too early to {what}: no default root window")
RuntimeError: Too early to create image: no default root window
Exception ignored in: <function PhotoImage.__del__ at 0x000002E86DDEF380>
Traceback (most recent call last):
File "C:\Users\Asus\AppData\Local\Programs\Python\Python311\Lib\site-packages\PIL\ImageTk.py", line 132, in __del__
name = self.__photo.name
^^^^^^^^^^^^
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Код:
from tkinter import *
import random
from PIL import ImageTk
im=ImageTk.PhotoImage(file="C:/Users/Asus/Desktop/program/rubik/cube.png")
im2=ImageTk.PhotoImage(file="C:/Users/Asus/Desktop/program/rubik/Cube1.png")
room=Tk()
room.geometry('200x200')
room.resizable(height=False,width=False)
room.mainloop()
btn=Button(room, image=im, height=100, width=100)
Источник: Stack Overflow на русском