python模塊調(diào)用練習(I)
還是兩個不同文件種模塊調(diào)用的問題。
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<width):
? ? ? ? count=count+grid[row-1][col+1]
? ? if (col-1>=0):
? ? ? ? count=count+grid[row][col-1]
? ? if ?(col+1<width):
? ? ? ? count=count+grid[row][col+1]
? ? if (row+1<height):
? ? ? ? count=count+grid[row+1][col]
? ? if (row+1<height) and (col-1>=0):
? ? ? ? count=count+grid[row+1][col-1]
? ? if (row+1<height) and (col+1<width):
? ? ? ? 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()
雖然沒有顯示怎么實現(xiàn)功能,但是調(diào)用成功。
####第一次根據(jù)書本嘗試。