Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help with canvas widget in Tkinter please

209 views
Skip to first unread message

Ivan Van Laningham

unread,
Jan 1, 2000, 3:00:00 AM1/1/00
to Python Mailing List
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.
iva...@callware.com
iva...@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
----------------------------------------------


flounder

unread,
Jan 2, 2000, 3:00:00 AM1/2/00
to

Fredrik Lundh

unread,
Jan 2, 2000, 3:00:00 AM1/2/00
to flounder
flounder <flounder...@telebot.com> wrote:
> 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

what documentation?

your problem, and how to fix it, is described here:
http://www.pythonware.com/library/tkinter/introduction/x131-more-on-widget-references.htm

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
(currently number five on fatbrain's bestseller list! ;-) -->

Gerrit Holl

unread,
Jan 2, 2000, 3:00:00 AM1/2/00
to flounder
flounder wrote on 946779607:

> display = Canvas(mw, width=640, height=480).pack()
>
> item = display.create_image(0, 0, anchor=NW, image=photo)

That's not a TKinter related problem. You're not creating a class instance,
as you might think. .pakc() doesn't return an instance. Try:


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

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

...
display.pack()

regards,
Gerrit.

--
"The move was on to 'Free the Lizard'"

-- Jim Hamerly and Tom Paquin (Open Sources, 1999 O'Reilly and Associates)
11:27pm up 12:26, 13 users, load average: 0.01, 0.05, 0.00


0 new messages