我刚刚尝试运行我的 python 脚本并收到无效的语法错误我已经仔细检查了问题但仍然看不到问题可以看到问题吗?如果有人可以帮助查看问题,下面是代码。
谢谢。
import random
import Tkinter
class life (Tkinter.Frame):
def __init__(self,x,y):
Tkinter.Frame.__init__(self)
self.master.title("The Game Of Life")
self.grid = [[False for j in range (y)] for i in range (x)]
universe = Tkinter.Frame(self)
def createButton (i,j):
bitmap=None
if self.grid[i][j]:bitmap ='gray75'
gridEntry = Tkinter.Button(universe, bitmap=bitmap)
gridEntry.grid(row=i, column =j)
self._applyToEachCellOfGrid (createButton)
universe.pack(side='top')
scale= Tkinter.Scale(self, orient = Tkinter.HORIZONTAL, from_=1, to =10,
command=self.setPeriod)
scale.pack(side='top')
quit=Tkinter.Button(self, text='Quit', command=self.quit)
quit.pack(side='left')
run=Tkinter.Button(self, text='Run' , command=self.run)
run.pack(side='right')
pause=Tkinter.Button(self, text='Pause', command=self.pause)
pause.pack(side='right')
self.pack()
def _applyToEachCellOfGrid (self, function) :
for i in range (len(self.grid)):
for j in range (len(self.grid[i])):
function(i,j)
def populateGridRandomly(self,x):
random.seed()
def setRandomly(i,j):
if random.random()<x:self.grid[i][j]= True
self._applyToEachCellOfGrid(setRandomly)
def calculateNextGeneration(self):
newGrid=[[ False for j in range ( len (self.grid[i]))] for i in range ( len (self.grid))]
def calculateLiveness(x,y):
count = 0
for i in range (x-1, x+2):
if 0 <=i<len (self.grid[i]):
if ((i !=x)or(j !=y)) and self.grid[i][j]:count +=1
if self.grid[x][y]:return 2 <= count <=3
else: return count ==3
def setNewGrid(x,y):
newGrid[x][y]= calculateLiveness(x,y)
self._applyToEachCellOfGrid (setNewGrid)
self.grid = newGrid
def pause(self):
print 'pause called.'
def run(self):
print 'run called'
def setPeriod (self, value):
print 'setPeriod called with value' , value
if __name__ =="__main__':
game=Life(8,10)
game.mainloop()