newbie, re: tkinter canvas
Locash
built4living at excite.com
Fri Mar 7 12:07:47 EST 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
imgD).
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
happens?
I want to change:
"self.emptyWorld[x].append(imgD)" to "self.emptyWorld[x].append(0)"
and
"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)
self.grid()
self.createWidgets()
def createWidgets(self):
imgD = PhotoImage(file='death.gif')
self.world = Canvas (self, height=406, width=406, bg=
"#eeeeee", cursor="hand1")
self.world.grid (row=0, column=0, columnspan=6)
self.world.bind ("<Button-1>", self.createLife)
for x in range (0,40):
self.emptyWorld.append([])
self.currentGen.append([])
for y in range (0,40):
self.world.create_image(((x+1)*10), ((y+1)*10),
image=imgD)
self.emptyWorld[x].append(imgD)
self.currentGen = self.emptyWorld
self.world.img=imgD
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):
self.world.create_image(x, y, image=imgL)
self.currentGen[i][j] = imgL
else: pass
self.world.img = 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