0

When I am trying to run the following piece of code, it is showing error:
Traceback (most recent call last):
_tkinter.TclError: couldn't recognize data in image file "logo.png"
Code:

from Tkinter import *


root = Tk()
root.geometry('300x200')
f0 = Frame(root,width=300, height=50)
f0.pack()
logo = PhotoImage(file='logo.png')
lb1 = Label(f0,image=logo).pack()

f1 = Frame(root)
f1.pack()
label = Label(f1, text="Email")
label.grid(row=1,column=1)

wb1 = Entry(f1, bd = 5)
wb1.grid(row=1,column=2)
label2 = Label(f1,text="Password")
label2.grid(row=2,column=1) 
wb2 = Entry(f1,show='*',bd = 5)
wb2.grid(row=2,column=2)
label3 = Label(f1,text="Bug ID")
label3.grid(row=3,column=1)
wb3 = Entry(f1, bd = 5)
wb3.grid(row=3,column=2)

f2 = Frame(root) 
f2.pack()

def printdata():
     st = "Hello, Work in progress.."
     lab2 = Label(f2, text=st)
     lab2.grid(row=2, column=2)


but1 = Button(f2,text="Automate it", bg="blue", fg="white", 
command=printdata)
but1.grid(row=1,column=2)
root.mainloop()

Can someone suggest, what's the possible mistake I am doing here? Thanks in advance.

Chanfool21
  • 57
  • 9
  • check if you have the image in your working directory – AD WAN May 16 '18 at 10:38
  • Hi AD WAN, its in the same directory. Once I changed its location just to verify then it showed error like " Couldn't find this file". But this error is something different. FYI I am using Python 2.7 – Chanfool21 May 16 '18 at 10:44
  • 1
    PNG files are not supported by `PhotoImage`. See https://stackoverflow.com/a/27601573/3714930 – fhdrsdg May 16 '18 at 10:46
  • You need a very recent Tkinter to load PNG files directly. Otherwise, use `ImageTk.PhotoImage` from PIL (pillow). – PM 2Ring May 16 '18 at 10:51
  • Just tried with Jpeg too :( tkinter.TclError: couldn't recognize data in image file "gg.jpeg" – Chanfool21 May 16 '18 at 10:53
  • I read `Photoimage` doesn't support `PNG` but when i use it displays on label and button i think it will the version of `Tkinter` – AD WAN May 16 '18 at 10:55
  • As the accepted answer of the linked question says, Tkinter only supports GIF, PPM or PGM. That was true until Tkinter 8.6. But using PIL you will have access to a wide range of image formats. – PM 2Ring May 16 '18 at 10:56
  • restart your `ide` and run the code again – AD WAN May 16 '18 at 10:56

2 Answers2

1

I think this should fix it (more details on this reference):

from PIL import Image, ImageTk

image = Image.open("logo.png")
logo = ImageTk.PhotoImage(image)

lb1 = Label(f0,image=logo)
lb1.image = logo #keep a reference to it
lb1.pack()

EDIT:

I think your tkinter version is able to run this directly:

logo = ImageTk.PhotoImage("logo.png")

lb1 = Label(f0,image=logo)
lb1.image = logo #keep a reference to it
lb1.pack()
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • 1
    You don't need to open and convert, it can be done in one step: `ImageTk.PhotoImage(file="logo.png")` – PM 2Ring May 16 '18 at 10:52
0

Remove your pack to newline

logo = PhotoImage(file='logo.png')
lb1 = Label(f0,image=logo)
lbl.pack()
AD WAN
  • 1,414
  • 2
  • 15
  • 28
  • Same error. I even changed the image, just to verify if the image has corrupted data or not. But didnt work – Chanfool21 May 16 '18 at 10:50
  • 2
    While I agree that placing `pack()` on a new line is good practice since it returns `None`, I don't think it solves the problem in this question. The error is most likely thrown on the line `logo = PhotoImage(file='logo.png')` – fhdrsdg May 16 '18 at 10:50