python - 如何解决python2.7中的名称错误

标签 python python-2.7 tkinter

当我在 python3 中运行 Python 脚本时,它运行良好,但在 python 2.7 中我收到错误,我应该在 Python 2.7 中运行此代码:

这是我的代码:我想测试一些寻路算法,并希望创建一个由网格组成的窗口,在其中单击时拖动鼠标以创建墙壁并通过右键单击并拖动来删除它们

import Tkinter

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()

错误:

NameError: name 'Canvas' is not defined

如何更改我的代码以便在 Python 2.7 中运行它?

最佳答案

嗯,它没有定义好,我想知道它如何在 Py3 上运行,除非 Tkinter 以某种方式将 Canvas 转储到你的命名空间中。

您可能想做 class CellGrid(Tkinter.Canvas),但我不记得 Tkinter 的确切结构,所以它可能在子包或其他东西中。

关于python - 如何解决python2.7中的名称错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47226530/

相关文章:

python - 使用 str(count) 追加到列表会出错

python - Selenium :无法下载文件

python - 为什么 api.depends 不能在非编辑模式下使用 message_follower_ids 字段?如何修复它?

python - 如何使用 Django 1.8 将参数从基于类的 View 传递到表单?

python - 如何使用 python evdev 向设备发送隆隆声效果

C++ 客户端需要找出套接字被扭曲的服务器关闭

python - .after() 方法递归

python - 使窗口关闭特定值(python)

python - 返回 tkinter 列表框中选定值的列表

python - 找不到 appcfg.py 或 dev_appserver.py?