Need help with canvas widget in Tkinter please

Ivan Van Laningham ivanlan at callware.com
Sat Jan 1 23:02:23 EST 2000


Hi Flounder

flounder wrote:
> 
> I cannot seem to figure out how to add images to a canvas with python/Tkinter
> 
> I can do it with Tcl/Tk and Perl/Tk but not with python i have tried this but
> 
> it did not work at all. Here is the code i used
> 
> -----------CODE--------------
> 
> #!/usr/bin/python
> 
> from Tkinter import *
> 
> mw = Tk()
> mw.title("Map Editor")
> photo = PhotoImage(file="tile0.gif")
> 
> display = Canvas(mw, width=640, height=480).pack()
> 
> item = display.create_image(0, 0, anchor=NW, image=photo)
> 
> image = display.addImage
> mainloop()
> 
> --------------END OF CODE---------------
> 
> when i run this it gives me this error
> 
> -----------ERROR MESSAGE--------------
> 
> Traceback (innermost last):
>   File ".//test.py", line 11, in ?
>     item = display.create_image(0, 0, anchor=NW, image=photo)
> AttributeError: 'None' object has no attribute 'create_image'
> 
> -----------END OF ERROR---------------
> 
> can anyone show me how this should be done I have looked at the
> 
> documentation and can't figure it out
> 
> Thanks in advance
> 
> --
> Flounder
> 
> --
> http://www.python.org/mailman/listinfo/python-list

#!/usr/bin/python

from Tkinter import *

mw = Tk()
mw.title("Map Editor")
photo = PhotoImage(file="tile0.gif")

display = Canvas(mw, width=640, height=480)

item = display.create_image(0, 0, anchor=NW, image=photo)

display.pack()
mainloop()

----------------------------------------------
Your main problem was calling Canvas(...).pack(), since the pack()
method returns None.

And no need to add_image(), either.  create_image() does everything you
need.

<can't-add-things-to-None>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
Author:  Teach Yourself Python in 24 Hours
----------------------------------------------




More information about the Python-list mailing list