【问题标题】:Type Error: Missing one positional argument "self" Conway's Game of Life类型错误:缺少一个位置参数“自我”康威的生命游戏
【发布时间】:2014-03-19 21:47:36
【问题描述】:

我一直在尝试一个小程序来模拟康威的生命游戏。但是错误TypeError: step() missing 1 required positional argument: 'self' 不断出现。我一直在寻找解决方案,但我尝试的任何方法似乎都不起作用。

这是我的代码:

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)

def GridStep():
    Grid.step()

def GridClear():
    Grid.step()

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=GridStep)
    buttonStep.pack(side = LEFT)
    buttonClear = Button(bottomFrame, text = "Clear", command=GridClear)
    buttonClear.pack(side=LEFT , after=buttonStep)
    root.mainloop()

【问题讨论】:

标签: python python-3.x


【解决方案1】:

您有两个直接在类上调用未绑定方法的函数:

def GridStep():
    Grid.step()

def GridClear():
    Grid.step()

Grid 是类本身,而不是类的实例,调用Grid.step() 不会提供self 参数,因为没有实例可以绑定self。您需要在此处提供Grid 的实例并调用 that 上的方法。

您需要从您创建的grid 对象中传递方法,而不是在按钮上使用GridStepGridClear

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)

请注意,我将绑定方法传递给command,但它们尚未(尚未)被调用。

然后您可以完全删除GridStepGridClear 函数。

【讨论】:

  • 感谢您的帮助,但现在当我尝试在 stepToNext 状态函数中说 AttributeError: 'Cell' object has no attribute 'nextState' 时出现新错误,我不明白。
  • 在调用setNextState 之前,您的Cell 实例没有nextState 属性,即使这样,也只有在满足某些条件的情况下。如果您不完全理解这应该如何工作,也许最好问一个新的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 2010-09-07
  • 2017-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多