canvas in Pmw

Peter Leveille psl at mitre.org
Fri May 12 14:12:09 EDT 2000


Thanks.  That did it.  I should have realized that was the problem,  but
sometimes the answer is just to obvious to grasp :)

Thanks for you time,
Pete

John Grayson wrote:
> 
> Mike,
> 
> Bingo!
> 
> The offending line is:
> 
>         img = PhotoImage(file='amap.gif')
> 
> This is a common error, and is because img is local to the createoutput
> method. When you return from the method, img goes out of scope and is
> garbage collected -- hence the blank image.
> 
> Simple (at least for the single instance) solution:
> 
>          self.img = PhotoImage(file='amap.gif')
>          map.create_image(200,200, image = self.img, anchor = CENTER)
> 
> In case you are not aware, it is NOT necessary to make map an attribute
> of the instance: Tkinter keeps an internal reference which ensures that
> it does not get garbage collected.
> 
> If this solves the problem, please post the answer back to
> comp.lang.python.
> This is a fairly common problem, but it never hurts to repeat the
> answer.
> 
>     Cheers!
> 
>       John
> 
> Mike Wingfield wrote:
> >
> > Woops, sorry. That last email included some newly introduced errors :(
> >
> > Mike Wingfield wrote:
> > >
> > > Well my code is pretty large.  Here is a code fragment that isn't
> > > working:
> > >
> > > from Tkinter import *
> > > import ODBC.Windows
> > > import Pmw
> > > import string
> > > root = Tk()
> > > Pmw.initialise()
> > >
> > > class CPGUI(Frame):
> > >     "This is the main Conflict Probe GUI class"
> > >     def __init__(self, parent=None):
> > >         Frame.__init__(self)
> > >         self.option_add('*Font', 'Verdana 8 bold')
> > >         self.pack(expand=YES, fill=BOTH)
> > >         self.master.title('CPGUI')
> > >         self.master.iconname('CPGUI')
> > > .
> > > .
> > > .
> > > .
> > >         probepackrefresh = Button(pbuttonbox, text="Refresh", font =('Verdana
> > > bold',                          7), command=self.refreshprobepacklist)
> > >         probepackrefresh.pack(side=LEFT, pady = 0)
> > > .
> > > .
> > > .
> > > .
> > >     def createoutput(self):
> > >         "called to display reported conflicts from conflict table"
> > >         map = ScrolledCanvas(root, width = 600, height = 400)
> > >         img = PhotoImage(file='amap.gif')
> > >         map.create_image(200,200, image = img, anchor = CENTER)
> > >         map.pack(expand=YES, fill=BOTH)
> > >         map.resizescrollregion()
> > > .
> > > .
> > > .
> > > CPGUI().mainloop()
> > >
> > > This gives me the following error:
> > >
> > > Traceback (innermost last):
> > >   File "CPGUI.py", line 325, in ?
> > >     CPGUI().mainloop()
> > >   File "CPGUI.py", line 70, in __init__
> > >     probepackrefresh = Button(pbuttonbox, text="Refresh", font
> > > =('Verdana bold', 7), command = self.refreshprobepacklist)
> > >   File "C:\Program Files\Python\Lib\lib-tk\Tkinter.py", line 1123, in
> > > __init__
> > >     Widget.__init(self, master, 'button', cnf, kw)
> > >   File "C:\Program Files\Python\Lib\lib-tk\Tkinter.py", line 1078, in
> > > __init__
> > >     BaseWidget._setup(self, master, cnf)
> > >   File "C:\Program Files\Python\Lib\lib-tk\Tkinter.py", line 1058, in
> > > _setup
> > >     self.tk = master.tk
> > > AttributeError: tk
> > >
> > > John Grayson wrote:
> > > >
> > > > In article <391B0669.578876FF at mitre.org>,
> > > >   Mike Wingfield <psl at mitre.org> wrote:
> > > > >
> > > > > I am trying to write an image to a canvas in an application using Pmw.
> > > > > When I do this I get the following error message:
> > > > >
> > > > > Traceback (innermost last):
> > > > >   File "OutWin.py", line 24, in ?
> > > > >     map.create_image(200,200, image=img, anchor = CENTER)
> > > > >   File "C:\Program Files\Python\Lib\lib-tk\Tkinter.py", line 1223, in
> > > > > _create_image
> > > > >     return self.create('image', args, kw)
> > > > >   File "C:\Program Files\Python\Lib\lib-tk\Tkinter.py", line 1216, in
> > > > > _create
> > > > >     (self.w, 'create', itemType)
> > > > > TclError: image "8827440" doesn't exist
> > > > >
> > > > > If I remove Pmw parts of the code it seems to work ok.  What do I need
> > > > > to do to get the canvas to run within a Frame in an application using
> > > > > Pmw?
> > > > >
> > > >
> > > > To get a good response from this group you really need to provide
> > > > at least a fragment of your code -- otherwise you'll tend to get
> > > > responses such as:
> > > >
> > > >       "Yep, it doesn't work -- that's an error message"      :(
> > > >
> > > > This fragment works:
> > > >
> > > > import Pmw
> > > > root = Tk()
> > > > Pmw.initialise()
> > > >
> > > > sc = Pmw.ScrolledCanvas(root, borderframe=1, labelpos=N,
> > > >                         label_text='ScrolledCanvas', usehullsize=1,
> > > >                         hull_width=400, hull_height=300)
> > > > img = PhotoImage(file='img52.gif')
> > > > sc.create_image(145,280, image=img, anchor=CENTER)
> > > > sc.pack()
> > > > sc.resizescrollregion()
> > > >
> > > > root.mainloop()
> > > >
> > > > Sent via Deja.com http://www.deja.com/
> > > > Before you buy.



More information about the Python-list mailing list