newbie, re: tkinter canvas

Locash built4living at
Fri Mar 7 18:07:47 CET 2003

I have written a script to model Conway's Game of Life.  Currently it
prints successive generations to the console.  But I now want to wrap
it in a GUI.  So far I have created a canvas and populated it with
images of 'dead' cells.  Then the user picks which cells to bring to
life in the initial generation.  Graphically this code does what I
want, but in addition I want to store the information about the cells
status (alive or dead) to a list using the values 1 or 0.  At the
moment, the image that exists in a cell is being stored (imgL or

When I make the following changes, suddenly every pick I make wipes
all other images from the canvas.  Can anyone explain to me why this

I want to change:

"self.emptyWorld[x].append(imgD)" to "self.emptyWorld[x].append(0)"


"self.currentGen[i][j] = imgL" to "self.currentGen[i][j] = 1"

from Tkinter import *

class Application(Frame):
    emptyWorld = []
    currentGen = []
    def __init__(self, master=None):
        Frame.__init__(self, master)
    def createWidgets(self):        
        imgD = PhotoImage(file='death.gif')
        = Canvas (self, height=406, width=406, bg=
"#eeeeee", cursor="hand1") (row=0, column=0, columnspan=6) ("<Button-1>", self.createLife)
        for x in range (0,40):
            for y in range (0,40):      
      *10), ((y+1)*10),
                self.currentGen = self.emptyWorld      
    def createLife(self, event):
        imgL = PhotoImage(file='life.gif')
        x = int(((event.x + 5)/10))*10
        y = int(((event.y + 5)/10))*10
        for i in range (0, len(self.currentGen)):
            for j in range (0, len(self.currentGen[i])):
                if x == (i*10) and y == (j*10):                     
          , y, image=imgL)
                    self.currentGen[i][j] = imgL                    
                else: pass = imgL                       
app = Application() # Instantiate the application class
app.master.title("Conway's Game of Life")
app.mainloop() # Wait for events

Thanks in advance for your help!

More information about the Python-list mailing list