Stupid Tkinter question

Charles G Waldman cgw at fnal.gov
Mon Apr 12 17:07:04 EDT 1999


Bjoern Giesler writes:

 > I'm going crazy. Why does this fragment work (adapted from imageview.py
 > in Guido's Tkinter demos):
 >  [snip]
 > .... but this doesn't:
 >  [snip]
 >
 > def makeToolbar(aFrame):
 >   toolbar = Frame(aFrame)
 >   markimage = BitmapImage(file = "Mark.xbm")
 >   markbutton = Button(toolbar,
 >                       image = markimage,
 >                       command = exit)
 >
 >   markbutton.pack()
 >   return toolbar

The "markimage" object goes out of scope when makeToolbar exits, and
storage for the image automatically gets cleared up.  You need to
cause there to be a persistent reference to the image object.
Fortunately this is pretty easy to do.  Try this:

def makeToolbar(aFrame):
   toolbar = Frame(aFrame)
   toolbar.markimage = BitmapImage(file = "Mark.xbm")
   markbutton = Button(toolbar,
                       image = toolbar.markimage,
                       command = exit)

   markbutton.pack()
   return toolbar




More information about the Python-list mailing list