【问题标题】:Attribute error: Object has no attribute Python属性错误:对象没有属性 Python
【发布时间】:2014-05-14 16:50:08
【问题描述】:

我正在尝试编写康威人生游戏的一个版本,但是当它似乎应该在要求引用它之前声明 nextState 的值时,我不断收到消息 'Cell' Object Has no attribute 'nextState'。这是我的代码:

from tkinter import *
root = Tk()

class Cell (Button):
    Dead = 0
    Live = 1

    def __init__ (self,parent):
        Button.__init__(self,parent, relief = "raised" , width = 2 , borderwidth = 1 , command = self.onpress)
        self.displayState(Cell.Dead)

    def onpress (self):
        if self.state == Cell.Live:
            self.displayState(Cell.Dead)
        elif self.state == Cell.Dead:
            self.displayState(Cell.Live)

    def setNextState (self , Neighbours):
        if self.state == Cell.Live and (Neighbours < 2 or Neighbours > 3):
            self.nextState = Cell.Dead
        elif self.state == Cell.Dead and Neighbours == 3:
            self.nextState = Cell.Live
        elif self.state == Cell.Dead and Neighbours != 3:
            self.nextState = self.state

    def stepToNextState(self):
        self.displayState(self.nextState)

    def displayState (self , newstate):
        self.state = newstate
        if self.state == Cell.Live:
            self["bg"] = "black"
        if self.state == Cell.Dead:
            self["bg"] = "white"

class Grid:
    def __init__(self,parent,sizex,sizey):
        self.sizex = sizex
        self.sizey = sizey
        self.cells = []
        for a in range (0,self.sizex):
            rowcells = []
            for b in range (0, self.sizey):
                c = Cell(parent)
                c.grid(row=b , column=a)
                rowcells.append(c)
            self.cells.append(rowcells)

    def step (self):
        cells = self.cells
        for x in range (0,self.sizex):
            if x==0: x_down = self.sizex-1
            else: x_down = x-1
            if x==self.sizex-1: x_up = 0
            else: x_up = x+1
            for y in range(0,self.sizey):
                if y==0: y_down = self.sizey-1
                else: Y_down = y-1
                if y==self.sizey-1: y_up = 0
                else: y_up = y+1
                sum = cells[x_down][y].state + cells[x_up][y].state + cells[x][y_down].state + cells[x][y_up].state + cells[x_down][y_down].state +cells[x_up][y_up].state + cells[x_down][y_up].state + cells[x_up][y_down].state
                cells[x][y].setNextState(sum)
            for row in cells:
                for cell in row:
                    cell.stepToNextState()

    def clear(self):
        for row in self.cells:
            for cell in row:
                cell.displayState(Cell.Dead)

if __name__ == "__main__":
    frame = Frame(root)
    frame.pack()
    grid = Grid(frame,25,25)
    bottomFrame = Frame(root)
    bottomFrame.pack (side = BOTTOM)
    buttonStep = Button(bottomFrame , text="Step" , command=grid.step)
    buttonStep.pack(side = LEFT)
    buttonClear = Button(bottomFrame, text = "Clear", command=grid.clear)
    buttonClear.pack(side=LEFT , after=buttonStep)
    root.mainloop()

错误信息是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
    return self.func(*args)
  File "C:\Users\Owner\Desktop\Other\Programs & Misc\Python\Tyler's Game of Life.py", line 65, in step
    cell.stepToNextState()
  File "C:\Users\Owner\Desktop\Other\Programs & Misc\Python\Tyler's Game of Life.py", line 27, in stepToNextState
    self.displayState(self.nextState)
AttributeError: 'Cell' object has no attribute 'nextState'

如果有人能指出错误发生的位置/原因以及可能的解决方法,我将不胜感激。

【问题讨论】:

  • 看起来你有很多缩进问题,正确的缩进在 Python 中至关重要。这只是复制到 SO 时的错误吗?
  • 我缩进了你的问题,然后如果工作正常......
  • @shaktimaan 如果他的问题是缩进,请不要在问题中更正它——将其作为答案发布。
  • @Barmar:我只是将问题缩进让其他人理解并帮助解决问题,之后我运行它并瞧……不知道这是唯一的问题
  • 这是我在第一条评论中提出问题的重点。重要的是要知道他的实际代码是否正确缩进。就 Python 而言,缩进不仅仅是可读性问题,它还会影响代码是否正确。

标签: python tkinter attributeerror conways-game-of-life


【解决方案1】:

问题似乎是您的for row in cells循环在您之前的@​​ 987654322 @循环中。这是step方法应该看起来像正确缩进的话:

def step (self):
    cells = self.cells
    for x in range (0,self.sizex):
        if x==0: x_down = self.sizex-1
        else: x_down = x-1
        if x==self.sizex-1: x_up = 0
        else: x_up = x+1
        for y in range(0,self.sizey):
            if y==0: y_down = self.sizey-1
            else: Y_down = y-1
            if y==self.sizey-1: y_up = 0
            else: y_up = y+1
            sum = cells[x_down][y].state + cells[x_up][y].state + cells[x][y_down].state + cells[x][y_up].state + cells[x_down][y_down].state +cells[x_up][y_up].state + cells[x_down][y_up].state + cells[x_up][y_down].state
            cells[x][y].setNextState(sum)
    for row in cells:                     # unindent these
        for cell in row:                  # lines by one
            cell.stepToNextState()        # level each

如果所有的缩进问题都在处理(或不是原始代码中),仍然存在一个问题 5月 EM>导致某些情况下的问题。问题是Cell.setNextState方法无法处理每种情况。具体而言,如果一个单元格活着,则不会设置nextState,应该保持所以(它有两个或三个生物邻居)。 if 987654328 @和elif elif @ @ 987654329应该把它作为一个红色的标志为我提出,但我第一次审视了这个功能时忽略了它。

这是如何修复的:

def setNextState (self , Neighbours):
    if self.state == Cell.Live and (Neighbours < 2 or Neighbours > 3):
        self.nextState = Cell.Dead
    elif self.state == Cell.Dead and Neighbours == 3:
        self.nextState = Cell.Live
    else: # remove the conditions on this block, all the state changes were handled above
        self.nextState = self.state

【讨论】:

  • 尝试未解压缩后,我仍然收到相同的错误。 'Cell" object has no attribute 'nextState'。 span>
  • @ chubzorz:我找到了剩下的问题并编辑了我的答案。 span>
猜你喜欢
  • 1970-01-01
  • 2014-07-08
  • 2020-05-25
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
相关资源
最近更新 更多