Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem displaying images with TkInter

Reply
Thread Tools

Problem displaying images with TkInter

 
 
David
Guest
Posts: n/a
 
      03-04-2004
Hi,
I'm new to python so I'm probably making a fairly simple mistake, but
look at the example below. It simply displays a rectangle and an image.

-------------------------------------------------
from Tkinter import *

root = Tk()

c = Canvas(root, width=1024, height = 1024)
c.pack()

c.create_rectangle(10,10,50,50)
mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
c.create_image(50, 50, anchor=NW, image=mudPhoto)

root.mainloop()
-----------------------------------------------

Now I try to modify it so that the image and rectangle only appear when I
click the mouse on the canvas. I use the following code:

-----------------------------------------------
from Tkinter import *

root = Tk()

def OnClick(event):
print "click!!!"
c.create_rectangle(10,10,50,50)
mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
c.create_image(50, 50, anchor=NW, image=mudPhoto)

c = Canvas(root, width=1024, height = 1024)
c.pack()
c.bind("<Button-1>",OnClick)
root.mainloop()
----------------------------------------------

However when I click the mouse it prints 'click!!!' and draws the rectangle
as expected. However it doesn't draw the image! Does anyone have any idea
why this would be?

Thanks for any help,

David
 
Reply With Quote
 
 
 
 
Eric Brunel
Guest
Posts: n/a
 
      03-04-2004
David wrote:
> Hi,
> I'm new to python so I'm probably making a fairly simple mistake, but
> look at the example below. It simply displays a rectangle and an image.
>
> -------------------------------------------------
> from Tkinter import *
>
> root = Tk()
>
> c = Canvas(root, width=1024, height = 1024)
> c.pack()
>
> c.create_rectangle(10,10,50,50)
> mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
> c.create_image(50, 50, anchor=NW, image=mudPhoto)
>
> root.mainloop()
> -----------------------------------------------
>
> Now I try to modify it so that the image and rectangle only appear when I
> click the mouse on the canvas. I use the following code:
>
> -----------------------------------------------
> from Tkinter import *
>
> root = Tk()
>
> def OnClick(event):
> print "click!!!"
> c.create_rectangle(10,10,50,50)
> mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
> c.create_image(50, 50, anchor=NW, image=mudPhoto)
>
> c = Canvas(root, width=1024, height = 1024)
> c.pack()
> c.bind("<Button-1>",OnClick)
> root.mainloop()
> ----------------------------------------------
>
> However when I click the mouse it prints 'click!!!' and draws the rectangle
> as expected. However it doesn't draw the image! Does anyone have any idea
> why this would be?


This used to be a FAQ, but I can't it anymore (anyone knows where it has gone?).

The problem lies in the fact that for some reason, the Canvas where you do the
create_image does *not* keep a reference on the actual PhotoImage object. So
this object disappears when your variable mudPhoto goes out of scope (i.e. at
the end of your OnClick function), which causes its deletion at tk level. So
when OnClick finishes, the canvas finds itself displaying an image that doesn't
exist anymore.

The workaround is simple: always keep an explicit reference on all your
PhotoImage and BitmapImage objects until you no more need them. For your
particular case, this can be done via:

def OnClick(event):
print "click!!!"
c.create_rectangle(10,10,50,50)
c.mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
c.create_image(50, 50, anchor=NW, image=c.mudPhoto)

Turning mudPhoto into an attribute of the canvas ensures that it won't be
deleted before the canvas is.

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      03-05-2004
Ok, that's working now. Thanks for the tip!

David

Eric Brunel wrote:

> David wrote:
>> Hi,
>> I'm new to python so I'm probably making a fairly simple mistake,
>> but
>> look at the example below. It simply displays a rectangle and an image.
>>
>> -------------------------------------------------
>> from Tkinter import *
>>
>> root = Tk()
>>
>> c = Canvas(root, width=1024, height = 1024)
>> c.pack()
>>
>> c.create_rectangle(10,10,50,50)
>> mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
>> c.create_image(50, 50, anchor=NW, image=mudPhoto)
>>
>> root.mainloop()
>> -----------------------------------------------
>>
>> Now I try to modify it so that the image and rectangle only appear when I
>> click the mouse on the canvas. I use the following code:
>>
>> -----------------------------------------------
>> from Tkinter import *
>>
>> root = Tk()
>>
>> def OnClick(event):
>> print "click!!!"
>> c.create_rectangle(10,10,50,50)
>> mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
>> c.create_image(50, 50, anchor=NW, image=mudPhoto)
>>
>> c = Canvas(root, width=1024, height = 1024)
>> c.pack()
>> c.bind("<Button-1>",OnClick)
>> root.mainloop()
>> ----------------------------------------------
>>
>> However when I click the mouse it prints 'click!!!' and draws the
>> rectangle as expected. However it doesn't draw the image! Does anyone
>> have any idea why this would be?

>
> This used to be a FAQ, but I can't it anymore (anyone knows where it has
> gone?).
>
> The problem lies in the fact that for some reason, the Canvas where you do
> the create_image does *not* keep a reference on the actual PhotoImage
> object. So this object disappears when your variable mudPhoto goes out of
> scope (i.e. at the end of your OnClick function), which causes its
> deletion at tk level. So when OnClick finishes, the canvas finds itself
> displaying an image that doesn't exist anymore.
>
> The workaround is simple: always keep an explicit reference on all your
> PhotoImage and BitmapImage objects until you no more need them. For your
> particular case, this can be done via:
>
> def OnClick(event):
> print "click!!!"
> c.create_rectangle(10,10,50,50)
> c.mudPhoto = PhotoImage(file="/root/metropolis/mud.gif")
> c.create_image(50, 50, anchor=NW, image=c.mudPhoto)
>
> Turning mudPhoto into an attribute of the canvas ensures that it won't be
> deleted before the canvas is.
>
> HTH


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory problem with displaying images k.mucha4@chello.pl Java 2 03-07-2007 06:07 AM
Displaying formatted - data with TKinter custard_pie Python 1 05-02-2005 11:03 AM
Displaying HTML Files from Python (Using Tkinter) Fuzzyman Python 0 01-28-2004 09:25 AM
problem displaying large images with thumbnails Cloud Burst HTML 8 01-14-2004 12:56 AM
Re: displaying video in a tkinter GUI Jim Easterbrook Python 0 08-22-2003 08:38 AM



Advertisments