Embedding an Image in a Tkinter Canvas widget using PIL
I'm trying to embed an image in a Tkinter canvas using PIL, and for some reason it's not displaying. Any Ideas?
There are no errors or warnings. Just a blank Tk window.
My code:
import Tkinter
from PIL import ImageTk, Image
class image_manip(Tkinter.Tk):
def __ini开发者_StackOverflowt__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.ImbImage = Tkinter.Canvas(self)
self.ImbImage.pack()
i = ImageTk.PhotoImage(Image.open('test.png'))
self.ImbImage.create_image(0, 0, image=i)
def run():
image_manip(None).mainloop()
if __name__ == "__main__":
run()
My guess is that the image is getting garbage-collected. Try saving a reference to "i" (eg: self.image=i)
精彩评论