Python 类型错误:缺少一个位置参数";“自我”;康威';人生的游戏

Python 类型错误:缺少一个位置参数";“自我”;康威';人生的游戏,python,python-3.x,Python,Python 3.x,我一直在尝试一个小程序来模拟康威的人生游戏。但是,错误TypeError:step()缺少1个必需的位置参数:“self”会继续出现。我一直在寻找解决办法,但我尝试的方法似乎都不管用 这是我的密码: from tkinter import * root = Tk() class Cell (Button): Dead = 0 Live = 1 def __init__ (self,parent): Button.__init__(self,parent

我一直在尝试一个小程序来模拟康威的人生游戏。但是,错误
TypeError:step()缺少1个必需的位置参数:“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()
从tkinter导入*
root=Tk()
类单元格(按钮):
死=0
现场=1
定义初始化(自身,父级):
按钮。uuu初始化(self,parent,relief=“raised”,width=2,borderwidth=1,command=self.onpress)
self.displayState(Cell.Dead)
def onpress(自):
如果self.state==Cell.Live:
self.displayState(Cell.Dead)
elif self.state==单元格死:
self.displayState(Cell.Live)
def setNextState(自身、邻居):
如果self.state==Cell.Live和(邻居<2或邻居>3):
self.nextState=单元格。已死亡
elif self.state==单元死亡和邻居==3:
self.nextState=Cell.Live
elif self.state==单元格。死亡和邻居!=三:
self.nextState=self.state
def stepToNextState(自身):
self.displayState(self.nextState)
def显示状态(自身、新闻状态):
self.state=newstate
如果self.state==Cell.Live:
自我[“bg”]=“黑色”
如果self.state==Cell.Dead:
自我[“背景”]=“白色”
类别网格:
定义初始值(自身、父项、sizex、sizey):
self.sizex=sizex
self.sizey=sizey
self.cells=[]
对于范围内的(0,self.sizex):
行单元格=[]
对于范围(0,self.sizey)中的b:
c=单元(父单元)
c、 网格(行=b,列=a)
行单元格。追加(c)
self.cells.append(行单元格)
def步骤(自我):
细胞=自身细胞
对于范围内的x(0,self.sizex):
如果x==0:x_down=self.sizex-1
否则:x_down=x-1
如果x==self.sizex-1:x_up=0
否则:x_up=x+1
对于范围内的y(0,self.sizey):
如果y==0:y\u down=self.sizey-1
否则:Y_down=Y-1
如果y==self.sizey-1:y\u up=0
其他:y_up=y+1
sum=cells[x_down][y]。state+cells[x_down][y_down]。state+cells[x][y_down]。state+cells[x_down][y_down]。state+cells[x_down][y_up]。state+cells[x_up][y_down]。state
单元格[x][y].setNextState(总和)
对于单元格中的行:
对于行中的单元格:
cell.stepToNextState()
def清除(自):
对于self.cells中的行:
对于行中的单元格:
cell.displayState(cell.Dead)
def GridStep():
Grid.step()
def GridClear():
Grid.step()
如果名称=“\uuuuu main\uuuuuuuu”:
帧=帧(根)
frame.pack()
网格=网格(框架,25,25)
底部框架=框架(根)
bottomFrame.pack(侧面=底部)
buttonStep=按钮(底部框架,text=“Step”,command=GridStep)
按钮阶梯式包装(侧面=左侧)
buttonClear=按钮(底部框架,text=“Clear”,command=GridClear)
buttonClear.pack(侧面=左侧,后面=按钮步骤)
root.mainloop()

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

def GridStep():
    Grid.step()

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

您不需要在按钮上使用
GridStep
GridClear
,而需要从创建的
grid
对象传递方法:

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)
请注意,我正在将绑定的方法传递给
命令
,但它们尚未被调用


然后可以完全删除
GridStep
GridClear
函数。

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

def GridStep():
    Grid.step()

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

您不需要在按钮上使用
GridStep
GridClear
,而需要从创建的
grid
对象传递方法:

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)
请注意,我正在将绑定的方法传递给
命令
,但它们尚未被调用


您可以完全删除
GridStep
GridClear
函数。

我不是Pythonista,但看起来
step
是一种实例方法;您正在类
网格中调用它。请您的问题将显示错误的全文,以便我们可以看到它发生的位置。可能重复的I'm not a Pythonista,但它看起来像
step
是一个实例方法;您正在类
网格中调用它。请将您的问题记录在错误的全文中,以便我们可以看到错误发生的位置。可能重复感谢您的帮助,但是现在我遇到了一个新的错误,当我尝试这样说时,
AttributeError:“Cell”对象在stepToNext State函数中没有属性“nextState”
,我不理解。在调用
setNextState
之前,您的
Cell
实例没有
nextState
属性,即使如此,也只有在满足某些条件的情况下。如果您不完全理解这是如何工作的,也许最好问一个新问题。谢谢您的帮助,但现在我在尝试说
AttributeError:“Cell”对象在stepToNext State函数中没有属性“nextState”
,这是一个新的错误,我不理解。您的
Cell
实例