-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_proto2.py
81 lines (59 loc) · 2.05 KB
/
view_proto2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from tkinter import *
import model
cell_size = 5
is_running = False
def setup():
global root, grid_view, cell_size, start_button, choice
root = Tk()
root.title('The Game of Life')
grid_view = Canvas(root,
width=model.width*cell_size,
height=model.height*cell_size,
borderwidth=0,
highlightthickness=0,
bg='white')
start_button = Button(root, text='Start', width=12)
start_button.bind('<Button-1>', start_handler)
clear_button = Button(root, text='Clear', width=12)
choice = StringVar(root)
choice.set('Choose a Pattern')
option = OptionMenu(root, choice, 'Choose a Pattern', 'glider', 'glider gun', 'random')
option.config(width=20)
grid_view.grid(row=0, columnspan=3, padx=20, pady=20)
start_button.grid(row=1, column=0, sticky=W, padx=20, pady=20)
option.grid(row=1, column=1, padx=20)
clear_button.grid(row=1, column=2, sticky=E, padx=20, pady=20)
def start_handler(event):
global is_running, start_button
if is_running:
is_running = False
start_button.configure(text='Start')
else:
is_running = True
start_button.configure(text='Pause')
update()
def update():
global grid_view, root, is_running
grid_view.delete(ALL)
model.next_gen()
for i in range(0, model.height):
for j in range(0, model.width):
if model.grid_model[i][j] == 1:
draw_cell(i, j, 'black')
if is_running:
root.after(100,update)
def draw_cell(row, col, color):
global grid_view, cell_size
if color == 'black':
outline = 'gray'
else :
outline = 'white'
grid_view.create_rectangle(row*cell_size,
col*cell_size,
row*cell_size+cell_size,
col*cell_size+cell_size,
fill=color, outline=outline)
if __name__ == '__main__':
setup()
update()
mainloop()