Stumped by PhotoImage issue -- Think I've got it now

Joe joekm at
Thu Feb 28 03:34:06 CET 2002

Managed to solve the problem described below by tying the "OK" button
to a separate function in the "About" class 
[ i.e. def buttonAction(self, event=NONE)]

Anyway, once I did that, the PhotoImage generated labels would
display.  I'm guessing that when Tk() finishes up, PhotoImage releases
its gifs.  What I was lead to believe on the net was that they needed
to be defined in a persistant way.  So I was trying to make them class
variables (self.keepMeAround], declaring them global, defining them
outside of the class then passing them to it and then redefining them
in the __init__ method, and other similar approaches.

The only clue other than that was discovering that if I broke the
button on purpose, the gifs would display (but the button would not).
So, not really thinking it would work I made this change:

instead of:

self.OK = Button(yada yada, command=AboutDialog.destroy)

I did this:

self.OK = Button[yada yada, command=self.buttonAction]

and then wrote a short function to destroy the About dialog window.

I'm guessing that this keeps a pending operation on the Tk() widget
which, in turn, keeps PhotoImage from loosing track of the gifs.  As
it happens, all the other times I've used PhotoImage in this program
the buttons all had functions defined separately.

......Anyway, that worked but if any of you programming experts out
there can add further clarification I'd appreciate it.  

On Tue, 26 Feb 2002 18:22:11 GMT, joekm at (Joe) wrote:

>I've actually seen that there are others with a similar problem that
>have posted on the web.  I've read G.R's FAQ and looked at the
>solutions others have used but I am still confused.....
>In a program that I am writing, I have an "about" dialog box that is
>written as a class that is in turned called from the "help : about"
>pulldown menu item in my "menubar" class.  This "about" dialog has
>three .gif images that are handled by PhotoImage.  The problem is that
>they appear to be displaying transparant.
>Now, if I pull the class out of my main program and wrap a minimalist
>mainloop around it, everything works fine.  Also, if I intentionally
>put an error in the part of the class that draws the button, the gif's
>are drawn without a problem (but I get an error message and the button
>does not draw).  
>I've tried setting instance variables, global variables, etc. and I'm
>running out of ideas.  Any suggestions would be appreciated.  Just so
>you know, this is my first original python program and my first real
>intro into object oriented programming.  OTOH, I have read everything
>I could download and purchased a couple of books on the subject
>including "Python and Tkinter Programming".
>..and no, I do not wish to use PMW at this time, I wish to gain
>experience with Tkinter first.
>The class follows (needs to be un-word-wrapped):
>class About:
>    def __init__(self, parent, GraphicsPath):
>        self.parent = parent
>        self.deltaLogo = PhotoImage(file=GraphicsPath+'DeltaLogo.gif')
>        self.pythonLogo =
>        self.tkLogo = PhotoImage(file=GraphicsPath+'TCL-TK_logo.gif')
>        AboutDialog = Toplevel(self.parent, relief=RAISED,
>        AboutDialog.geometry('+300+150')
>        AboutDialog.overrideredirect(1)
>        AboutDialog.tkraise()
>        self.AboutBorder = Frame(AboutDialog, relief=GROOVE,
>        title = Label(self.AboutBorder, text='Simple Airframe Fracture
>Evaluation Tool [SAFE-T]',
>                             font=('Ariel', 11, 'bold'))
>        title.pack(side=TOP, padx=2, pady=5)
>        global Revision_Type, Revision
>        version = Label(self.AboutBorder, text=Revision_Type+'
>        version.pack(pady=5)
>        copyright1 = Label(self.AboutBorder, text='Copyright 200X by
>Joseph K. Mooney and')
>        copyright1.pack(pady=5)
> = Label(self.AboutBorder, image=self.deltaLogo)
>        copyright2 = Label(self.AboutBorder, text='ALL RIGHTS
>RESERVED\nSee License Agreement for Further Information\n')
>        copyright2.pack()
>        description = Label(self.AboutBorder, text="Programed in
>PYTHON - GUI  built with Tkinter")
>        description.pack(pady=8)
>        self.python = Label(self.AboutBorder, image=self.pythonLogo)
>        self.python.pack(side=LEFT) 
>        self.tclTk = Label(self.AboutBorder, image=self.tkLogo)
>        self.tclTk.pack(side=RIGHT) 
>        self.AboutBorder.pack(fill=X, expand=YES)
>        self.OK = Button(self.AboutBorder, text='OK', font=('Arial',
>9, 'bold'), command=AboutDialog.destroy)
>, rely=.8, anchor=NW)

More information about the Python-list mailing list