Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
image error
#1
Photo 
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.
Reply
#2
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()
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "cannot identify image file" part way through running hatflyer 0 683 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  pygame image load error Yegor123 1 1,593 Oct-12-2022, 05:36 AM
Last Post: deanhystad
  python-resize-image unicode decode error Pedroski55 3 3,500 Apr-21-2020, 10:56 AM
Last Post: Pedroski55
  Error when executing pytesseract to get the text from image bvdinesh 3 17,942 Oct-19-2019, 05:01 PM
Last Post: cwco00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020