Python Forum
image error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: image error (/thread-41822.html)



image error - pyc0de - Mar-23-2024

hi im new to python and I'm having trouble getting this code to run. Instead I get this error, in tkinter . I'm using visual studio 2022 and I'm trying to add a PNG image to the program, I created a new solution and copy/pasted the PNG pic to a new folder within the solution

failed to create an iconphoto with image "icon.png"

my code so far
from tkinter import *   
            
window = Tk()
window.geometry("420x420")
window.title("Bro Coding")

icon = PhotoImage('icon.png')
window.iconphoto(True,icon)

window.mainloop()
This is my first python script, so I'm not against learning more.


RE: image error - deanhystad - Mar-23-2024

The documentation for Photoimage is terrible. I think that is why most people use PIL to load images. You need to use file='icon.png' to set the file where the image is read
import tkinter as tk  # Do not use wildcard imports  
             
window = Tk()
window.geometry("420x420")
window.title("Bro Coding")
 
icon = PhotoImage(file='icon.png')
window.iconphoto(True, icon)
 
window.mainloop()



RE: image error - pyc0de - Mar-23-2024

Thank you, i figured i was doing something wrong. I'll keep this site in mind while I'm learning to write python. I'm sure I'll need help. Cool