python tkinter image "…" doesn't exist error

Question:

from tkinter import *
from PIL import ImageTk, Image

window = Tk()
window.geometry("350x670")

topBar = Frame(window, bg= "black", width=350, height=70).pack()
middleBar = Frame(window, bg= "grey", width=350, height=530).pack()
botBar = Frame(window, bg= "black", width=350, height=70).pack()

imgLabel1 = Label(topBar, image="profile-pic.jpg").place(x=50,y=50)


window.mainloop()

This is my code. I want to set an image in topBar frame. When I run the code, I get this error:

_tkinter.TclError: image "profile-pic.jpg" doesn’t exist>

How can I solve this error? Thank you

Asked By: Yiğit Yılmaz

||

Answers:

You need to pass an instance of ImageTk.PhotoImage() to the image option of Label widget:

...
image = ImageTk.PhotoImage(file="profile-pic.jpg")
imgLabel1 = Label(topBar, image=image)
imgLabel1.place(x=50, y=50)
...
Answered By: acw1668
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.