[Tutor] Displaying image in scrolled canvas
Michael P. Reilly
arcege@speakeasy.net
Mon, 30 Apr 2001 19:23:34 -0400 (EDT)
kromag@nsacom.net wrote
>
> I am attempting to place a large .gif file into a scrolled canvas. I am
> working from the examples in programming python (in case the code looked
> slightly familiar :-)
>
> To wit:
Try these changes :
> from Tkinter import *
> class ScrolledCanvas(Frame):
> def __init__(self, parent=None, color='white'):
> Frame.__init__(self, parent)
> self.pack(expand=YES, fill=BOTH)
> photo=PhotoImage(file='\windows\desktop\wacky3.gif')
self.photo=PhotoImage(file='\\window\\desktop\\wacky3.gif')
> canv = Canvas(self, bg=color, relief=SUNKEN)
> canv.config(width=1010, height=745)
> canv.config(scrollregion=(0,0,300, 1000))
> canv.create_image(10,10, image=photo, anchor=NW)
canv.create_image(10,10, image=self.photo, anchor=NW)
> sbar = Scrollbar(self)
> sbar.config(command=canv.yview)
> canv.config(yscrollcommand=sbar.set)
> sbar.pack(side=RIGHT, fill=Y)
> canv.pack(side=LEFT, expand=YES, fill=BOTH)
> if __name__ == '__main__': ScrolledCanvas().mainloop()
>
> results in the properly-sized frame and scrollbar, but for some reason the
> image does not pop to life. What am I missing here?
Image objects in Tkinter need to have the reference kept. Because of a
interaction between Tk and Tkinter, when the actual object is destroyed
the Tk image is destroyed as well.
-Arcege
--
+----------------------------------+-----------------------------------+
| Michael P. Reilly | arcege@speakeasy.net |