I am making a pokemon battle game for a school project. I am resizing images to fit in the GUI and then placing them in the GUI. However, I am getting an error when trying to display the image in the GUI.
This is the error:
Traceback (most recent call last):
File "c:\Users\\\Desktop\pokemon\game.py", line 106, in <module>
GUI()
File "c:\Users\\\Desktop\pokemon\game.py", line 70, in __init__
GUI.carousel(self)
File "c:\Users\\\Desktop\pokemon\game.py", line 86, in carousel
self.bulbsaur = ImageTk.PhotoImage(file=self.b_file_n)
File "C:\Users\\\Desktop\pokemon\venv\lib\site-packages\PIL\ImageTk.py", line 103, in __init__
image = _get_image_from_kw(kw)
File "C:\Users\\\Desktop\pokemon\venv\lib\site-packages\PIL\ImageTk.py", line 59, in _get_image_from_kw
return Image.open(source)
File "C:\Users\\\Desktop\pokemon\venv\lib\site-packages\PIL\Image.py", line 3140, in open
prefix = fp.read(16)
File "C:\Users\\\Desktop\pokemon\venv\lib\site-packages\PIL\Image.py", line 517, in __getattr__
raise AttributeError(name)
AttributeError: read
Exception ignored in: <function PhotoImage.__del__ at 0x000001514C866EE0>
Traceback (most recent call last):
File "C:\Users\\\Desktop\pokemon\venv\lib\site-packages\PIL\ImageTk.py", line 133, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
My Code:
class GUI(): #define the GUI
def __init__(self): #__init__ is the first thing that will be called.
self.label = Label(root, text="Welcome to Pokemon Battle Game!")
self.label2 = Label(root, text="Please do not resize the window.")
self.label.place(x=2, y=2)
self.label2.place(x=2, y=20)
GUI.carousel(self)
def carousel(self):
def moveRight(self):
pass
def moveLeft(self):
pass
self.b_file = Image.open('bulbsaur.png')
self.c_file = Image.open('chamander.png')
self.s_file = Image.open('squirtle.png')
self.b_file_n = self.b_file.resize([50,50])
self.c_file_n = self.c_file.resize([50,50])
self.s_file_n = self.s_file.resize([50,50])
self.bulbsaur = ImageTk.PhotoImage(file=self.b_file_n)
self.chamander = ImageTk.PhotoImage(file=self.c_file_n)
self.squirtle = ImageTk.PhotoImage(file=self.s_file_n)
self.bulbsaur_label = Label(root, image=self.bulbsaur)
self.chamander_label = Label(root, image=self.chamander)
self.squirtle_label = Label(root, image=self.squirtle)
self.button_moveRight = Label(root, text="->", command=moveRight(self))
self.button_moveLeft = Label(root, text="<-", command=moveLeft(self))
self.bulbsaur_label.place(x=10, y=40)
if __name__ == "__main__": #if name = main is good practice when using classes. they prevent scripts from running when python is not properly initalised.
root.title("Pokemon! Battle Game v1") #set the title of the window
root.geometry("400x200") #set the size of the window
root.iconbitmap("./OIP-_2_.ico") #use an .ico file to set the icon of the window to a certain icon.
bg = PhotoImage(file="startingbackground.png")
background_label = Label(root, image=bg)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
GUI()
root.mainloop() #calls mainloop function to prevent the window from closing. acts as a while True loop for the whole code. thus while loops cannot be used in tkinter.
I have already tried the solutions detailed in:
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
but to no avail. How can I fix this?