-3

我刚刚尝试运行我的 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()
4

3 回答 3

5

如果您使用开头",则还必须使用结尾",而不是'

  File "synt2.py", line 63
    if __name__ =="__main__':
                            ^
SyntaxError: EOL while scanning string literal
于 2013-04-23T18:15:35.803 回答
0

这是你的代码,工作。

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()
于 2013-04-23T18:22:12.783 回答
0

life您在调用时声明了一个名为 的类Life。Python 区分大小写。将第 4 行更改为class Life (Tkinter.Frame):或将第 64 行更改为game=life(8,10)

于 2013-04-23T21:30:07.203 回答