What is the trick between these?

Peter Otten __peter__ at web.de
Mon Aug 15 15:57:45 EDT 2016


huey.y.jiang at gmail.com wrote:

> I am trapped by these two funny things:
> 
> class MyClass(upClass):
> 
>    def start(self):
>        ***do_menu_here****
> 
>        self.load_label_image()  # ---> this works
>        self.load_canvas_image()  # ---> this does not work
> 
>    def load_label_image(self):
>        img = PhotoImage(file="xx.gif")
>        Label(self, image=img).pack(side=BOTTOM, expand=YES, fill=BOTH)
>        return img
> 
>    def load_canvas_image(self):
>        img = PhotoImage(file="xx.gif")
>        self.canvas = Canvas(self, width=500, height=300, bg='white')
>        self.canvas.pack(expand=YES, fill=BOTH)
>        self.canvas.create_image(50, 0, image=img, anchor=NW)
> 
> if __name__ == '__main__':
>         root = Tk()
>         root.mainloop()
> 
> load_label_image() works. However, I cannot do BIND with it, because img
> has no method to do binding. So, I am trying load_canvas_image. Surprise,
> it does not work. These two defs are so similar, but Python complained
> that no attribute of load_canvas_image(). It is there, coexisting with
> load_label_image().
> 
> I cannot figure out what is wrong with it, can somebody tell me why?
> Thanks!

The only problem I see is that you do not keep a reference of the PhotoImage 
(as explained in MRAB's answer to your previous question).

What exception do you encounter? Do not paraphrase, copy and paste the 
actual traceback.




More information about the Python-list mailing list