Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Display an Image in Tkinter [duplicate]

When I run the program the canvas shows up but the Image does not.

canvas = Canvas(frame, width = 128, height = 128, bg= 'white')
    image_data = Image.open('NoArt.gif')
    ppm_f = ImageTk.PhotoImage(image_data)
    canvas.create_image(0, 0, image = ppm_f, anchor = NW)
    canvas.pack(side=BOTTOM)

any Ideas??

PS.

I have PIL ver 1.6, python 2.6, and The Version of Tkinter that comes with python 2.6

like image 466
Joshkunz Avatar asked Jul 29 '10 05:07

Joshkunz


2 Answers

Ok, I figured It out. Apparently due to the way that python deals with garbage disposal the pictures just get erased. A reference to the image in the global scope is required. This is the working code I eventually ended up using:

self.photo = PhotoImage(file="noart.ppm")
    self.Artwork = Label(self.frame, image=self.photo)
    self.Artwork.photo = self.photo
    self.Artwork.pack()

that self.Artwork.photo = self.photo is the important part. It ensures that the Image will be shown.

like image 181
Joshkunz Avatar answered Oct 13 '22 01:10

Joshkunz


As mentioned in this very answer and this more popular question & answer and also in effbot.page

When you add a PhotoImage or other Image object to a Tkinter widget, you must keep your own reference to the image object. If you don’t, the image won’t always show up.

The problem is that the Tkinter/Tk interface doesn’t handle references to Image objects properly; the Tk widget will hold a reference to the internal object, but Tkinter does not. When Python’s garbage collector discards the Tkinter object, Tkinter tells Tk to release the image. But since the image is in use by a widget, Tk doesn’t destroy it. Not completely. It just blanks the image, making it completely transparent…

The solution is to make sure to keep a reference to the Tkinter object,

For example the image in below code won’t always show up, if at all as it isn't declared in global scope:

import tkinter as tk

root = tk.Tk()

def show_image():
    img_label = tk.Label(root)
    image = tk.PhotoImage(file="test.png")
    img_label['image'] = image

    img_label.pack()


show_image()
root.mainloop()

whereas in the example below img_label.image is declared globally, as an attribute attached to our very img_label in this example, and will be shown:

import tkinter as tk

root = tk.Tk()

def show_image():
    img_label = tk.Label(root)
    img_label.image = tk.PhotoImage(file="test.png")
    img_label['image'] = img_label.image

    img_label.pack()


show_image()
root.mainloop()

It is though still strange as img_label isn't globally declared either. A less confusing way would be adding global image as the first line to the show_image.

like image 41
Nae Avatar answered Oct 13 '22 00:10

Nae