I have a simulation, where a create moves around a map. The map is drawn with tkinter. In the simulation's init I call
root = Tk()
root.geometry("800x800+300+300")
self.ex = land.Example(root)
root.mainloop()
to start tkinter, where land.Example is the class below. What should happen is the map is initially drawn, then the simulation runs and anytime the creature moves it will use self.ex to draw that in tkinter. The problem is, when it runs, the map is drawn initially, and I have to close it so that the rest of the simulation will run. In that case the whole thing breaks because the simulation is trying to use a canvas that doesn't exist anymore. How can I fix this so the map and simulation both stay running
Simulation self.ex interaction
def move(self, creature, target):
self.map[creature.x][creature.y].creatures.remove(creature)
self.ex.updateMap(creature.x, creature.y, "remove")
land.Example
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.parent.title("Board")
self.pack(fill=BOTH, expand=1)
self.canvas = Canvas(self)
self.upperX = 0
self.lowerX = 0
self.upperY = 0
self.lowerY = 0
for x, row in enumerate(landMass):
for y, cell in enumerate(row):
color = "grey"
if isinstance(landMass[x][y], Food):
color = "green"
elif isinstance(landMass[x][y], Water):
color = "blue"
elif isinstance(landMass[x][y], Shelter):
color = "black"
self.canvas.create_rectangle(50 * x , 50 * y , 50 * x + 50, 50 * y + 50,
outline=color, fill=color)
self.canvas.create_text(3 + 50 * x, 3 + 50 * y, anchor=NW, fill="white", text=landMass[x][y].elevation)
if color == "green":
self.canvas.create_text(3 + 70 * x, 3 + 50 * y, anchor=NE, fill="red", text=landMass[x][y].vegitation)
elif color == "black":
self.canvas.create_text(3 + 70 * x, 3 + 50 * y, anchor=NE, fill="orange", text=landMass[x][y].quality)
self.canvas.pack(fill=BOTH, expand=1)
def updateMap(self, x, y, action):
color = "grey"
if action == "append":
#DRAW THE CREATURE AT X,Y
elif action == "remove":
#DRAW THE ORIGINAL TILE AT X,Y
...
self.canvas.pack(fill=BOTH, expand=1)
Edit: Other Attempts
when I add the following to the Simulation's init() and add a function. The whole thing runs, but the tk canvas, show the end result, not the result as it is happening.
root = Tk()
root.geometry("800x800+300+300")
self.tkmap = land.Example(root)
root.after(2000, self.runSim)
root.mainloop()
#End of __init__()
def runSim(self):
for generation in range(self.lifeCycles):
self.simulate(generation)
return self.evolve()