我有一个看起来像这样的程序:
def aStar(theMap, app):
# Redraws the cell in the GUI
def updateCell(x,y, value):
theMap[y][x] = value
app.gui.drawCell(x,y,value)
# Do a lot of stuff which calls the method above
def solveScenario(scenario):
class App(threading.Thread):
def __init__(self, theMap, n, m, cellSize):
self.root = Tk()
self.root.protocol("WM_DELETE_WINDOW", self.callback)
self.gui = CellGrid(self.root,n,m,cellSize,theMap)
self.gui.pack()
threading.Thread.__init__(self)
self.start()
def callback(self):
self.root.quit()
def run(self):
self.root.mainloop()
prob = Problem(scenario)
app = App(prob.theMap, prob.n, prob.m, 40)
aStar()
app.root.close()
def main():
scenarios = # .. list of scenarios
for i in scenarios:
solveScenario(i)
class CellGrid(Canvas):
theMap = None
def __init__(self,master, rowNumber, columnNumber, cellSize, theMap):
Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber)
self.cellSize = cellSize
self.theMap = theMap
#print theMap
self.grid = []
for row in range(rowNumber):
line = []
for column in range(columnNumber):
line.append(Cell(self, column, row, cellSize, theMap[row][column]))
self.grid.append(line)
self.draw()
def draw(self):
for row in self.grid:
for cell in row:
cell.draw()
def drawCell(self, x, y, value):
cell = self.grid[y][x]
cell.value = self.theMap[y][x]
cell.draw()
class Cell():
colors = {
0: 'white', # untried
1: 'black', # obstacle
2: 'green', # start
3: 'red', # finish
4: 'blue', # open
5: 'gray', # closed
6: 'orange', # path
}
def __init__(self, master, x, y, size, value):
self.master = master
self.abs = x
self.ord = y
self.size= size
self.fill = "white"
self.value = value
def setValue(self, value):
self.value = value
def draw(self):
""" order to the cell to draw its representation on the canvas """
if self.master != None :
xmin = self.abs * self.size
xmax = xmin + self.size
ymin = self.ord * self.size
ymax = ymin + self.size
self.master.create_rectangle(xmin, ymin, xmax, ymax, fill=self.colors[self.value], outline = "black")
main()
如您所见,对于每个场景(问题),使用Tkinter绘制矩阵,该矩阵由A-Star算法实时更新。这可以正常工作,但是当从一个场景切换到另一个场景时,地图会发生变化。如何刷新Tkinter窗口?使用上面的代码,即使再次调用app.root.close()
和solveScenario()
,生成新地图时单元格也会保持颜色。
由于