Reputation: 197
I'm trying to display an image.
from tkinter import *
from tkinter import ttk
root = Tk()
myimage = PhotoImage(file='world.gif')
ttk.Label(root, image=myimage)
root.mainloop()
When I run this I expect to see my image. But I only see a solid grey image. Any help is appreciated.
Upvotes: 1
Views: 1242
Reputation: 550
You need to keep a reference to the Tkinter object. Something like this (I'm not using ttk here):
myimage = PhotoImage(file='world.gif')
label = Label(image=myimage)
label.image = myimage # the reference
label.pack()
The garbage collector is trying to discard the Tkinter image object since Tkinter doesn't keep a reference on it. So Tkinter releases the image, Tk widget doesn't, and you get a weird blank image.
Upvotes: 3