[Tutor] ImageTk problem

Harry Kattz gibbs05@flash.net
Fri, 23 Feb 2001 15:13:49 -0600


Greetings Rick & All,

I've been having the same kind problems with PIL.

I think Michael Reilly is correct about retaining a reference, though I
couldn't get his solution to work under Windows.

I could get Rick's example to work when I modified it as follows to match
examples by creating a subclass of Label to hold the image:

from Tkinter import *
import Image, ImageTk

class ImageLabel(Label):

    def __init__(self, master, im):

        self.im2 = ImageTk.PhotoImage(im)
        Label.__init__(self, master, image=self.im2)

class Main:
    def __init__(self,win):
        self.win = win
        im1 = Image.open("picture.gif")
        ImageLabel(self.win, im = im1).pack()
        b = Button(self.win,text='Quit',height=2)
        b.pack()
        b.config(command = lambda w = self.win: w.destroy())

def main():
    root = Tk()
    Main(root)
    root.mainloop()

if __name__ == '__main__':
    main()


I really don't know why this works and the other doesn't.


Good luck,
Sam


> The following code produces no error and no visible picture. The size of
> the picture is known since using a different image will change the size
> of the label. It doesn't matter whether it's a gif or a jpg.
>
> What am I overlooking?
>
> from Tkinter import *
> import Image, ImageTk
>
> class Main:
> def __init__(self,win):
> self.win = win
> im1 = Image.open("picture.gif")
> im2 = ImageTk.PhotoImage(im1)
> Label(self.win, image = im2).pack()
> b = Button(self.win,text='Quit',height=2)
> b.pack()
> b.config(command = lambda w = self.win: w.destroy())
>
> def main():
> root = Tk()
> Main(root)
> root.mainloop()
>
> if __name__ == '__main__':
> main()
>
> --
> "The financial policy of the welfare state requires that there be no
> way for the owners of wealth to protect themselves.  This is the
> shabby secret of the welfare statists' tirades against gold.   Deficit
> spending is simply a scheme for the 'hidden' confiscation of wealth.
> Gold stands in the way of this insidious process.   It stands as a
> protector of property rights."
> -- Alan Greenspan
>    Rick Pasotto email: rickp@telocity.com
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor