简体   繁体   中英

Problems displaying an image on Tkinter

I am using Tkinter with Python to create a GUI. I could not figure out how to display seaborn plots in tkinter (another problem), so I am trying to circumvent that by saving the plot as an image and then displaying it.

I have tried the following code to display an existing image:

from tkinter import *
from PIL import ImageTk, Image

window = Tk()

load = Image.open('plot.png')
render = ImageTk.PhotoImage(load)

img = Label(window, image=render)
img.image = render
img.place(x=0,y=0)

window.mainloop()

However, this gives me the error:

TclError: image "pyimage3" doesn't exist

I have searched the forum with regard to this but could only find an answer relating to having several instances of Tk() in the code, but I simplified my code to just the above and it is not working.

The image is correctly named and contained within the same folder as the Python file. The same error occurs if I try it with a different image in JPEG or GIF format. The same error also occurs if I try putting several lines into one command. I have Pillow installed.

from tkinter import*
from PIL import Image, ImageTk

root = Tk()
f1 = Frame(root,width = 900,height=700,relief=SUNKEN)
f1.pack(side=LEFT)

load = Image.open('mylogo.jpg')
render = ImageTk.PhotoImage(load)
img = Label(image=render)
img.image = render
img.pack(side=TOP)

Try this code. This code works for me. Thanks

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