【发布时间】:2017-01-05 17:41:22
【问题描述】:
我正在尝试创建一个程序,其中动物在环境中随机移动,存储在数组中。我也试图在 tkinter 画布上以图形方式表示这一点,但是画布根本不显示任何内容。从我读到的内容看来,我可以使用 'after' 方法来解决这个问题,但是我不知道如何将它集成到我的程序中。
任何帮助将不胜感激。
import random
from tkinter import*
class Animal():
def __init__(self,x,y):
self.x = x
self.y = y
class Enviroment():
def __init__(self):
self.GUI = GUIcontainer(self)
self.boardSize = 100
self.template = self.setUpTemplate()
self.GUI.setup()
def setUpTemplate(self):
template = []
for x in range(0,self.boardSize+1):
template.append([])
for y in range(0,self.boardSize+1):
template[x].append("N")
return template
def placeAnimals(self):
print("Placing")
for x in range(0,self.boardSize+1):
for y in range(0,self.boardSize+1):
if random.randint(0,10) == 5 and self.template == "N":
obj = Animal(x,y)
self.template[x][y] = obj
self.GUI.moveOrPlaceObj(obj,x,y)
def moveAnimals(self):
print("Moving")
for x in range(0,self.boardSize+1):
for y in range(0,self.boardSize+1):
if self.template[x][y] != "N":
obj = self.template[x][y]
dirction = random.randint(0,3)
if direction == 0 and y>5:
if self.template[x][y-5] == "N":
self.template[x][y-5] = obj
self.GUI.moveOrPlaceObj(obj,x,y-5)
self.template[x][y] = "N"
elif direction == 1 and x>5:
if self.template[x-5][y] == "N":
self.template[x-5][y] = obj
self.GUI.moveOrPlaceObj(obj,x-5,y)
self.template[x][y] = "N"
elif direction == 2 and y<95:
if self.template[x][y+5] == "N":
self.template[x][y+5] = obj
self.GUI.moveOrPlaceObj(obj,x,y+5)
self.template[x][y] = "N"
elif direction == 3 and x<95:
if self.template[x+5][y] == "N":
self.template[x+5][y] = obj
self.GUI.moveOrPlaceObj(obj,x+5,y)
self.template[x][y] = "N"
class GUIcontainer(object):
def __init__(self,enviro):
self.root = Tk()
self.Enviroment = enviro
self.widgetCreator = GUIwidgetCreator(self.root)
self.entryWidgets = []
self.statLabels = []
self.root.title("Simulation")
def setup(self):
self.widgetCreator.createButton("Start",(lambda event: self.startSim()),2,2)
self.canvas = self.widgetCreator.createCanvas(3,3,100,100,"black")
self.root.mainloop()
def startSim(self):
self.Enviroment.placeAnimals()
self.Enviroment.moveAnimals()
def moveOrPlaceObj(self,obj,x,y):
self.canvas.delete(obj)
self.canvas.create_line(x+0.5,y+0.5,x-0.5,y-0.5,fill="green",tag=obj)
#self.canvas.create_line(x+0.5,y+0.5,x-0.5,y-0.5,fill=obj.colour,tag=obj)
#self.canvas.after(100, self.moveOrPlaceObj)
self.canvas.update_idletasks()
class GUIwidgetCreator(object):
def __init__(self,root):
self.root = root
def createButton(self,txt,cmd,x,y):
self.button = Button(self.root)
self.button.config(text=txt)
self.button.bind("<Button-1>",cmd)
self.button.grid(column=x,row=y)
def createCanvas(self,x,y,height,width,colour):
self.canvas = Canvas(self.root)
self.canvas.config(height=height,width=width)
self.canvas.config(bg=colour)
self.canvas.grid(column=x,row=y)
return self.canvas
if __name__ == "__main__":
program = Enviroment()
【问题讨论】:
-
moveOrPlaceObj永远不会被调用,您可以通过在其中放置打印语句来检查。
标签: python python-3.x canvas tkinter tkinter-canvas