I’m trying to make an app using Python and Tkinter but I’m not able to load an image, or png to be specific. Instead, I’m getting this long error:
File "C:\Users\username\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "image1.png".
Can someone help me fix this? Here are the lines of code that might be causing the error:
cycle1IMG = tk.PhotoImage(file="image1.png")
def openNewWindow1():
newWindow = Toplevel(master)
newWindow.title("🚲Cycle 1")
newWindow.geometry("750x500")
Label(newWindow, text="Cycle 1", image= cycle1IMG).pack()
cycle1 =tk.Button(
root,
text="🚲Cycle 1",
command=openNewWindow1
)
cycle1.place(x=500,y=300)
>Solution :
Based on a comment to the question, it appears that you took a .webp file and just renamed it to .png. This doesn’t change the internal contents of the file, which is why tkinter reported the error: you told it to open a .png file but the contents of the file isn’t in the png format.
Tkinter doesn’t support webp. You’ll have to use some other library to read the file and convert it to a format supported by tkinter (eg: pillow)