Tkinter image not showing or giving an error
python, tkinter, user-interface
Solution
Bryan Oakley is correct, the image is not a jpg in terms of its content, even though your filesystem thinks it's a gif.
On my end I tried opening a jpg with your program and got the same error 'TclError: couldn't recognize data in image file "hello.jpg".'
So you can do this: Open your image with mspaint, then go to File > Save As and from the "Save As Type" dropdown, choose GIF. Then the code should work. This is what I used:
from Tkinter import *
root = Tk()
imgPath = r"hello.gif"
photo = PhotoImage(file = imgPath)
label = Label(image = photo)
label.image = photo # keep a reference!
label.grid(row = 3, column = 1, padx = 5, pady = 5)
root.mainloop()
(btw, if I changed line 7 above to `photo = PhotoImage(imgPath)` then like you, no image appears. So leave it as `photo = PhotoImage(file = imgPath)`)
Problem
I have tried two different things to try to get an image to show in a label ``` #This gives " TclError: couldn't recognize data in image file "TestImage.gif" " imgPath = "TestImage.gif" photo = PhotoImage(file = imgPath) label = Label(image = photo) label.image = photo # keep a reference! label.grid(row = 3, column = 1, padx = 5, pady = 5) ``` and ``` #This gives no error but the image doesn't show imgPath = "TestImage.gif" photo = PhotoImage(imgPath) label = Label(image = photo) label.image = photo # keep a reference! label.grid(row = 3, column = 1, padx = 5, pady = 5) ``` The image is in the same folder as all the code. Any suggestions on how to show an image?