还是两个不同文件种模块调用的问题。
1、算法模块。model.py
height=100
width=100
grid_model=[0]*height
next_grid_model=[0]*height
for i in range(height):
grid_model[i]=[0]*width
next_grid_model[i]=[0]*width
def next_gen():
global grid_model,next_grid_model
for i in range(0,height):
for j in range (0,width):
cell=0
print('check cell',i,j)
count=count_neighbours(grid_model,i,j)
if grid_model[i][j]==0:
if count==3:
cell=1
elif grid_model[i][j]==1:
if count==2 or count==3:
cell=1
next_grid_model[i][j]=cell
temp=grid_model
grid_model=next_grid_model
next_grid_model=temp
def count_neighbours(grid,row,col):
count=0
if (row-1>=0):
count=count+grid[row-1][col]
if (row-1>=0) and (col-1>=0):
count=count+grid[row-1][col-1]
if (row-1>=0) and (col+1
count=count+grid[row-1][col+1]
if (col-1>=0):
count=count+grid[row][col-1]
if (col+1
count=count+grid[row][col+1]
if (row+1
count=count+grid[row+1][col]
if (row+1=0):
count=count+grid[row+1][col-1]
if (row+1
count=count+grid[row+1][col+1]
return count
if __name__=='__main__':
next_gen()
2、视窗文件。view.py
from tkinter import *
import model
cell_size=5
def setup():
global root,grid_view,cell_size,start_btn,clear_btn,choice
root=Tk()
root.title('game screen')
grid_view=Canvas(root,width=model.width*cell_size,
height=model.height*cell_size,
borderwidth=0,
bg='white')
####这个部分开始引用model里面的信息
start_btn=Button(root,text="start",width=12)
clear_btn=Button(root,text="clear",width=12)
choice=StringVar(root)
choice.set('choose a pattern')
option=OptionMenu(root,choice,"'choose a pattern","grider","grider gun","random")
option.config(width=25)
####学习了怎么设置选择项
grid_view.pack()
start_btn.pack()
clear_btn.pack()
option.pack()
if __name__=='__main__':
setup()
mainloop()
虽然没有显示怎么实现功能,但是调用成功。
####第一次根据书本尝试。