尝试逐个构建生命游戏计划,我很困惑为什么无论我运行什么配置,右下方的矩形填充总是红色。此外,它还会读取配置文件,但会更改文件指示的任何方块的填充。
配置是文件格式的文本文件:
LIVE = 1
DEAD = 0
def board(canvas, width, height, n):
for row in range(n+1):
for col in range(n+1):
canvas.create_rectangle(row*height/n,col*width/n,(row+1)*height/n,(col+1)*width/n,width=1,fill='black',outline='green')
n = int(raw_input("Enter the dimensions of the board: "))
width = n*25
height = n*25
from Tkinter import *
import math
window=Tk()
window.title('Game of Life')
canvas=Canvas(window,width=width,height=height,highlightthickness=0)
canvas.grid(row=0,column=0,columnspan=5)
board = [[DEAD for row in range(n)] for col in range(n)]
rlist = [[None for row in range(n)] for col in range(n)]
print rlist
for row in range(n):
for col in range(n):
rlist[row][col] = canvas.create_rectangle(row*height/n,col*width/n,(row+1)*height/n,(col+1)*width/n,width=1,fill='black',outline='green')
f = open('configuration.txt','r')
for line in f:
parsed = line.split()
if len(parsed)>1:
i = int(parsed[0].strip())
j = int(parsed[1].strip())
board[row][col] = canvas.itemconfigure(rlist[row][col], fill='red')
window.mainloop()
答案 0 :(得分:3)
抱歉,我无法在你的问题中发表评论,但无论如何。
在最后一个forloop中你设置i和j,但是你从不使用它们,而是使用row和col。