0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    I can't reproduce this error (Python 2.7, Pillow 3.4.2). What versions are you using? – asongtoruin Jun 06 '17 at 16:36
  • Did you have a look https://stackoverflow.com/questions/20251161/tkinter-tclerror-image-pyimage3-doesnt-exist? – Rickson Jun 06 '17 at 21:24
  • I am using Python 3.6 and Pillow 4.1.1 @asongtoruin may it be worth uninstalling Python 3 and retrying it with Python 2 instead? – Jacqueline Jun 07 '17 at 05:45
  • @Rickson I did look at it, but that solution did not seem to apply to this problem as I only have one instance of Tk() in my code. I tried changing it to TopLevel() as the solution suggested but then I get the error "module 'tkinter' has no attribute 'TopLevel' " – Jacqueline Jun 07 '17 at 05:47
  • Update: embarrassingly, I misspelt "Toplevel". It now works using this command. Sorry about that. However, now, a redundant second window pops up - how do I get rid of this? I know I can use a xyz.withdraw() method, but I don't know how to "grab" this new window since I did not create it and it doesn't have a name? – Jacqueline Jun 07 '17 at 06:48

1 Answers1

0
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

  • An answer benefits from an explanation, what and why you changed things. This helps the OP and other readers to learn from it for future questions. – Mr. T Mar 18 '18 at 12:46