简体   繁体   中英

python tkinter image "..." doesn't exist error

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

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)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM