[Tutor] Displaying image in scrolled canvas [PhotoImage weirdness]

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 30 Apr 2001 14:41:05 -0700 (PDT)


On Mon, 30 Apr 2001 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:
> 
> 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')
>         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)
>         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?

The same question popped up last year too!

    http://mail.python.org/pipermail/tutor/2000-February/001002.html


You're right; it's not popping up.  For the life of me, I have no idea why
this isn't working.  What's weird is that:

###
root = Tk()
photo = PhotoImage(file="test.gif")
canv = Canvas(root)
canv.pack()
canv.create_image(0, 0, image=photo, anchor=NW)
mainloop()
###

works, but:

###
class MyFrame(Frame):
    def __init__(self, root):
        Frame.__init__(self, root)
        photo = PhotoImage(file="test.gif")
        canvas = Canvas(self)
        canvas.pack()
        canvas.create_image(0, 0, image=photo, anchor=NW)

root = Tk()
f = MyFrame(root)
f.pack()
mainloop()
###

doesn't!  I'm looking into this right now; in the meantime, has anyone
else run into the same thing?