Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 __init__(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()
like image 643
rectangletangle Avatar asked Feb 25 '23 06:02

rectangletangle


1 Answers

My guess is that the image is getting garbage-collected. Try saving a reference to "i" (eg: self.image=i)

like image 104
Bryan Oakley Avatar answered Mar 05 '23 15:03

Bryan Oakley