Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果我在不同的时间运行程序后,在不同的地方使用索引器,这意味着什么?_Python_Python 3.x_Animation_Canvas_Tkinter - Fatal编程技术网

Python 如果我在不同的时间运行程序后,在不同的地方使用索引器,这意味着什么?

Python 如果我在不同的时间运行程序后,在不同的地方使用索引器,这意味着什么?,python,python-3.x,animation,canvas,tkinter,Python,Python 3.x,Animation,Canvas,Tkinter,我试图为我自己的自我发展项目创作约翰·康威的《生活游戏》。 我遇到的一般问题是让动画在GUI上可视化,现在我收到的错误消息如下: Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“D:\Software\Python\lib\tkinter\\ uuuuu init\uuuuu.py”,第1705行,在调用中__ 返回self.func(*args) 文件“gameoflife.py”,第70行,在一个循环中 应用规则() 应用规则中第56行的文件“gameoflife.py”

我试图为我自己的自我发展项目创作约翰·康威的《生活游戏》。 我遇到的一般问题是让动画在GUI上可视化,现在我收到的错误消息如下:

Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“D:\Software\Python\lib\tkinter\\ uuuuu init\uuuuu.py”,第1705行,在调用中__ 返回self.func(*args) 文件“gameoflife.py”,第70行,在一个循环中 应用规则() 应用规则中第56行的文件“gameoflife.py” 更新的网格[行][列]=0 索引器:列表索引超出范围 但是如果我第二次运行它,我会在它原来说的错误所在的另一行上得到相同的错误。我知道实际的错误告诉我,我试图访问的索引不在列表中,但我不明白为什么它出现在下面的几行上,好像前一行已经被更正了。我的代码如下:

from tkinter import *
from random import *
import time
import numpy as np

PIXEL_SIZE = 10
ROW = 910
COLUMN = 700
grid = []
updated_grid = [[]]

def create_grid():
    for row in range(0, ROW):
        grid2 = []
        for column in range(0, COLUMN):
            grid2.append(randint(0, 1))
        grid.append(grid2)


def draw_grid():
    for row in range(0, ROW):
        for column in range(0, COLUMN):
            if grid[row][column] == 1:
                x0 = row*PIXEL_SIZE
                y0 = column*PIXEL_SIZE
                x1 = x0+PIXEL_SIZE
                y1 = y0+PIXEL_SIZE
                canvas.create_rectangle(x0, y0, x1, y1, fill='red')


def apply_rules():
    for row in range(1, ROW - 1):
        for column in range(1, COLUMN - 1):
            neighbours_count = 0
            # will count the neighbours for each cell
            neighbours_count += grid[row-1][column-1] # top left
            neighbours_count += grid[row][column-1] # top center
            neighbours_count += grid[row+1][column-1] # top right

            neighbours_count += grid[row-1][column] # middle left
            neighbours_count += grid[row+1][column] # middle right

            neighbours_count += grid[row-1][column+1] # bottom left
            neighbours_count += grid[row][column+1] # bottom center
            neighbours_count += grid[row+1][column+1] # bottom right

            # Game Of Life rules:

            # alive cell rules
            if grid[row][column] == 1:
                if neighbours_count < 2: # rule 1 any live cell with fewer than two live neighbours dies, as if by underpopulation
                    updated_grid[row][column] = 0
                elif neighbours_count == 2 | neighbours_count == 3: # rule 2 any live cell with two or three live neighbours lives on to the next generation
                    updated_grid[row][column] = 1
                elif neighbours_count > 3 & neighbours_count <= 8: # rule 3 any live cell with more than three live neighbours dies, as if by overpopulation
                    updated_grid[row][column] = 0
                else:
                    updated_grid[row][column] = 0
            elif grid[row][column] == 0: # dead cells rule 4 any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction
                if neighbours_count == 3:
                    updated_grid[row][column] = 1
                else:
                    updated_grid[row][column] = 0
    for row in range(0, ROW):
        for column in range(0, COLUMN):
            grid[row][column] = updated_grid[row][column]


def one_cycle():
    apply_rules()
    draw_grid()
    window.after(1, one_cycle)


window = Tk() # creates the window for the game
window.title('Game Of Life Python') # is the game title written on the window
canvas_frame = Frame(window) # creates a frame on the window to hold the canvas
game_title = Frame(window) # creates a frame on the window to display the game title (which will be a label)
start_button = Button(window, text='Start Game', command=one_cycle) # creates a button which will be used to start the game
canvas = Canvas(canvas_frame, width=ROW, height=COLUMN, background='black') # creates the canvas used to the draw the game of life
game_title_label = Label(game_title, text='Game Of Life', font='Helvetica 20 bold', fg='grey') # creates the label for the game title which will be placed in a frame

canvas.grid(row=0, column=0) # places the canvas onto the canvas_frame
canvas_frame.grid(row=1, column=1) # places the canvas_frame onto the window
game_title_label.grid(rowspan=2, column=0) # places the title of the game onto the game_title frame
game_title.grid(row=0, columnspan=2) # places the frame for the game title onto the window
start_button.grid(rowspan=2, column=1) # places the start onto the window


create_grid()
window.mainloop()
从tkinter导入*
从随机导入*
导入时间
将numpy作为np导入
像素大小=10
行=910
列=700
网格=[]
更新的_网格=[]]
def create_grid():
对于范围(0,行)中的行:
grid2=[]
对于范围(0,列)中的列:
grid2.append(randint(0,1))
grid.append(grid2)
def draw_grid():
对于范围(0,行)中的行:
对于范围(0,列)中的列:
如果网格[行][列]==1:
x0=行*像素大小
y0=列*像素大小
x1=x0+像素大小
y1=y0+像素大小
画布。创建_矩形(x0,y0,x1,y1,fill='red')
def应用规则():
对于范围(1,第-1行)中的行:
对于范围(1,列-1)中的列:
邻接数=0
#将计算每个单元的邻居
邻域数+=网格[第1行][第1列]#左上角
邻域数+=网格[行][列-1]#顶部居中
邻域数+=网格[行+1][列-1]#右上角
邻域数+=网格[第1行][列]#左中
邻域数+=网格[行+1][列]#右中
邻域数+=网格[第1行][第1列]#左下角
邻域数+=网格[行][列+1]#底部中心
邻居数+=网格[行+1][列+1]#右下角
#人生游戏规则:
#活细胞规则
如果网格[行][列]==1:
如果邻居的数量小于2:#规则1任何少于两个活邻居的活细胞都会死亡,就好像是由于人口不足
更新的网格[行][列]=0
elif neights_count==2 | neights_count==3:#规则2任何有两个或三个活邻居的活细胞都会延续到下一代
更新的网格[行][列]=1

elif neights_count>3&neights_count您从未填写
updated_grid
,因此无法将其指定给元素

程序启动时,应创建两个网格

def create_grid(ROW, COLUMN):
    grid = []
    for row in range(0, ROW):
        grid2 = []
        for column in range(0, COLUMN):
            grid2.append(randint(0, 1))
        grid.append(grid2)
    return grid

grid = create_grid(ROW, COLUMN)
updated_grid = create_grid(ROW, COLUMN)

您从未填写
updated_grid
,因此无法指定其元素

程序启动时,应创建两个网格

def create_grid(ROW, COLUMN):
    grid = []
    for row in range(0, ROW):
        grid2 = []
        for column in range(0, COLUMN):
            grid2.append(randint(0, 1))
        grid.append(grid2)
    return grid

grid = create_grid(ROW, COLUMN)
updated_grid = create_grid(ROW, COLUMN)

最简单的解决方案是复制现有网格并继续使用该网格:

import copy

def apply_rules():
    global grid
    updated_grid = copy.deepcopy(grid)
    # the rest of the function here, except the copying back again

    # This is all that's needed to 'copy' it back again:
    grid = updated_grid
通过这种方式,您可以从网格的副本开始:(
copy.deepcopy(grid)
)并按您所做的方式覆盖元素:(例如,
updated\u grid[row][column]=0
),最后通过引用计数的魔力处理旧网格并将新网格保留在一行:(
grid=updated\u grid


这是双缓冲的一种形式,最简单的解决方案是复制现有网格并继续使用该网格:

import copy

def apply_rules():
    global grid
    updated_grid = copy.deepcopy(grid)
    # the rest of the function here, except the copying back again

    # This is all that's needed to 'copy' it back again:
    grid = updated_grid
通过这种方式,您可以从网格的副本开始:(
copy.deepcopy(grid)
)并按您所做的方式覆盖元素:(例如,
updated\u grid[row][column]=0
),最后通过引用计数的魔力处理旧网格并将新网格保留在一行:(
grid=updated\u grid


这是一种
双缓冲形式

您需要预先分配
更新的网格
,否则通过索引访问其内容将无效,因为不存在索引。它需要类似于
网格
进行分配。我建议您更改
创建网格
以创建新网格并返回它,而不是附加到全局变量。然后你可以为
grid
updated_grid
@Barmar调用它谢谢你们的帮助你能不能让我明白你的意思?你需要预先分配
updated_grid
,否则通过索引访问它的内容将无效,因为没有索引。它需要类似地分配到
grid
。我建议您更改
create\u grid
以创建新的网格并返回它,而不是附加到全局变量。然后你可以为
grid
updated\u grid
@Barmar调用它。谢谢你们的帮助。你能不能让我明白你的意思?谢谢你的帮助,它已经摆脱了索引错误,但正在意外运行,所以现在我必须弄清楚,再次感谢Hanks的帮助,它已经摆脱了索引错误,但正在意外运行,所以现在我必须解决它,再次感谢