Cannot Display an Image in Tkinter [duplicate] Cannot Display an Image in Tkinter [duplicate] tkinter tkinter

Cannot Display an Image in Tkinter [duplicate]


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.


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 tkroot = 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 tkroot = 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.


If anyone else ever has a similar problem, you can try this hardcoded solution:

import Imageimport Tkinter as tkdef cleanhex(x):    return str(hex(x))[-2:]def renderimage(target,data,w,h):    canvas = tk.Canvas(target,width=w,height=h)    canvas.pack()    for x in range(w):        for y in range(h):            c = "#" + cleanhex(data[x,y][0]) + cleanhex(data[x,y][1]) + cleanhex(data[x,y][2])            canvas.create_line(x,y,x+1,y+1,fill=c)im = Image.open("whatever.whatever")renderimage(root,im.load(),im.size[0],im.size[1])