[Tkinter-discuss] Possibly n00bish question about ImageTk

Guilherme Polo ggpolo at gmail.com
Tue Feb 10 18:08:15 CET 2009


On Mon, Feb 9, 2009 at 7:01 PM, artencjo <artencjo at o2.pl> wrote:
>
> Hello!
> I know I just dug out a message from over a year, but I had exactly the same
> problem with my python and solved it quite easily...
> Just use command mainloop() after the program renders image. It refreshes
> the whole window, filling the gray frame with my graph ;)
> Greetings,
> Artur
>

I didn't understand your solution, how do you know if the image has
been rendered before calling mainloop ? Are you waiting for visibility
on the label or something like that ?

The more correct solution is to actually store a reference to the
ImageTk.PhotoImage instance (named "pi" in the example below),
otherwise the image will be deleted right after there are no more
references to it, which in the example given below will happen after
the function returns. I don't like this behaviour either, btw.

> Barnesdesdes wrote:
>>
>> I've been trying to make a program which will import a png file and
>> display it inside a frame.  It actually works when I just run the code,
>> but if I try to abstract the exact same code into a function, it fails.
>> In the code below, if I just run it, it works.  However, if I comment out
>> the lines between "root=Tkinter.Tk()" and "moo(root)" and comment in
>> "moo(root)", it no longer works.  Instead of displaying the image, it just
>> displays a gray frame of the correct size.  Does anyone have any insights
>> about what I'm doing wrong?
>>
>> import Image, ImageTk, Tkinter
>>
>> def moo(master):
>>     img = Image.open('plotfig.png')
>>     f = Tkinter.Frame(master, width=800, height=600)
>>     pi = ImageTk.PhotoImage(img)
>>     t = Tkinter.Label(f, image=pi)
>>     f.pack()
>>     t.place(x=0, y=0, width=800, height=600)
>>     t.pack()
>>
>> root = Tkinter.Tk()
>> img = Image.open('plotfig.png')
>> f = Tkinter.Frame(root, width=800, height=600)
>> pi = ImageTk.PhotoImage(img)
>> t = Tkinter.Label(f, image=pi)
>> f.pack()
>> t.place(x=0, y=0, width=800, height=600)
>> t.pack()
>> #moo(root)
>> root.mainloop()
>>
>>
>> Thanks very much.
>>
>> Alan
>>
>>
>
> --
> View this message in context: http://www.nabble.com/Possibly-n00bish-question-about-ImageTk-tp11330353p21921865.html
> Sent from the Python - tkinter-discuss mailing list archive at Nabble.com.
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>



-- 
-- Guilherme H. Polo Goncalves


More information about the Tkinter-discuss mailing list