While attempting to add images to a canvas programmatically, I wrote
the following:
for i in os.listdir('./icons/terrain'):
        self.terrainScreen["height"] = str(int(self.terrainScreen["height"])+50)
        debug(self.terrainScreen["height"]+" height of terrainScreen",5)
        img = PhotoImage(file='./icons/terrain/'+i)
        
self.terrainScreen.create_image(int(self.terrainScreen["height"])-50,(int(self.terrainScreen["height"])-50),
image=img,tag=None)

I couldn't figure out why it didn't work, until I went in and played
with Tkinter in the python shell, and I came to the conclusion that
the reason nothing was displayed on the canvas was that images are
passed by reference. In other words, unless there is a permanent `img'
variable, the data disappears.

For example (replace w\ your own gifs):
>>> from Tkinter import *
>>> root = Tk()
>>> terrain = Canvas(root,width=50,height=50)
>>> img = PhotoImage(file='MapEditor/icons/terrain/desert.gif')
>>> terrain.create_image(0,0,image=img)
1
>>> terrain.pack()

Works beautifully, eh?

>>> img = PhotoImage(file='MapEditor/icons/terrain/water.gif')

Doh! Now it disappears!

>>> terrain.create_image(0,50,image=img)
2

Working, but there is only one image

>>> terrain.create_image(0,0,image=PhotoImage('MapEditor/icons/terrain/grass.gif'))
3

Nothing gets displayed.

So there is my problem, and what is, I'm fairly certain, the cause of
the problem. Unfortunately, I really have no idea what the proper
solution should be. I'd like to populate the canvas programmatically,
but my brain seems to be choking on possible solutions. Suggestions?
Can I force variables to be passed by value?

-- 
--H.F.
My penguin is bigger than yours, mister...
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to