2015-05-04 80 views
0

我想测试一些路径查找算法,并想创建一个由网格组成的窗口,我可以在点击鼠标的同时拖动鼠标来创建墙,并通过右键单击并拖动来删除它们。我刚从计算机科学开始,对算法着迷,但我不知道如何为它创建交互式窗口。如何在Python中的窗口中创建交互式2D网格?

示例网格可以在这里看到: A* pathfindind visualisation

+0

你可以考虑使用tkinter,它允许在python中简单地创建简单的GUI。绑定到事件“onMouseClick”的画布应该没问题。玩的开心。 –

回答

2

这里来的溶液。您可以使用CellGrid.grid对象中包含的单元格对象列表的列表来应用您的寻路算法。玩的开心。

from tkinter import * 

class Cell(): 
    FILLED_COLOR_BG = "green" 
    EMPTY_COLOR_BG = "white" 
    FILLED_COLOR_BORDER = "green" 
    EMPTY_COLOR_BORDER = "black" 

    def __init__(self, master, x, y, size): 
     """ Constructor of the object called by Cell(...) """ 
     self.master = master 
     self.abs = x 
     self.ord = y 
     self.size= size 
     self.fill= False 

    def _switch(self): 
     """ Switch if the cell is filled or not. """ 
     self.fill= not self.fill 

    def draw(self): 
     """ order to the cell to draw its representation on the canvas """ 
     if self.master != None : 
      fill = Cell.FILLED_COLOR_BG 
      outline = Cell.FILLED_COLOR_BORDER 

      if not self.fill: 
       fill = Cell.EMPTY_COLOR_BG 
       outline = Cell.EMPTY_COLOR_BORDER 

      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 = fill, outline = outline) 

class CellGrid(Canvas): 
    def __init__(self,master, rowNumber, columnNumber, cellSize, *args, **kwargs): 
     Canvas.__init__(self, master, width = cellSize * columnNumber , height = cellSize * rowNumber, *args, **kwargs) 

     self.cellSize = cellSize 

     self.grid = [] 
     for row in range(rowNumber): 

      line = [] 
      for column in range(columnNumber): 
       line.append(Cell(self, column, row, cellSize)) 

      self.grid.append(line) 

     #memorize the cells that have been modified to avoid many switching of state during mouse motion. 
     self.switched = [] 

     #bind click action 
     self.bind("<Button-1>", self.handleMouseClick) 
     #bind moving while clicking 
     self.bind("<B1-Motion>", self.handleMouseMotion) 
     #bind release button action - clear the memory of midified cells. 
     self.bind("<ButtonRelease-1>", lambda event: self.switched.clear()) 

     self.draw() 



    def draw(self): 
     for row in self.grid: 
      for cell in row: 
       cell.draw() 

    def _eventCoords(self, event): 
     row = int(event.y/self.cellSize) 
     column = int(event.x/self.cellSize) 
     return row, column 

    def handleMouseClick(self, event): 
     row, column = self._eventCoords(event) 
     cell = self.grid[row][column] 
     cell._switch() 
     cell.draw() 
     #add the cell to the list of cell switched during the click 
     self.switched.append(cell) 

    def handleMouseMotion(self, event): 
     row, column = self._eventCoords(event) 
     cell = self.grid[row][column] 

     if cell not in self.switched: 
      cell._switch() 
      cell.draw() 
      self.switched.append(cell) 


if __name__ == "__main__" : 
    app = Tk() 

    grid = CellGrid(app, 50, 50, 10) 
    grid.pack() 

    app.mainloop() 
+0

我粘贴相同。我不得不用tkinter替换tkinter(tkkinter用小写't'由于某种原因不起作用)。我有这样的错误: 文件 “C:/Users/Pritam/PycharmProjects/RandomPythonProject/Randomfile.py”,1号线,在 进口的Tkinter 文件“C:\ Python27 \ LIB \ LIB-TK \ Tkinter的py”为38行,在 进口FixTk 文件 “C:\ Python27 \ lib中\ lib中-TK \ FixTk.py”,第65行,在 进口_tkinter 导入错误:DLL加载失败:%1不是一个有效的Win32应用程序。 处理完成退出代码1 –

+0

我用python 3编写了这个代码,这就是为什么有案例的问题。对于DLL相关的问题,似乎你的Python安装被打破...不能帮助这一点。 –