Today!, I'm trying to create an image viewer app using tkinter, the code I've written seems to get the error _tkinter.TclError: couldn't recognize data in image file "system_drive/boat.jpg"
. But besides that, to my knowledge there shouldn't be a reason why I am getting this error, the file boat.jpg
exists in the folder system_drive
and I've not made any syntactical errors either, Thanks for the help!
Code:
from tkinter import *
import os
tk = Tk()
image_list = []
for file in os.listdir(os.getcwd()):
if file.endswith(".png") or file.endswith(".jpg"):
image_list.append(PhotoImage(file))
file_name = Entry(tk, width=20,font=("Helvetica", 20))
canvas = Canvas(tk, width=500, height=500)
canvas.pack()
def click_show_img(x):
global file_name
global image_list
print("opening system_drive/{0}".format(file_name.get()))
img = PhotoImage(file="system_drive/{0}".format(file_name.get()))
canvas.create_image(60,60, anchor=NW, image=img)
tk.bind("<Return>", click_show_img)
file_name.pack()
tk.mainloop()
Screenshots:
How the window looks like:
How it should look like: