from tkinter import *
def step_while():
steps = int(ent_steps.get())
for i in range(steps):
root.after(700, step)
root.update()
def draw_a(event):
ii = event.y // cell_size
jj = event.x // cell_size
canvas.itemconfig(cell_matrix[jj][ii], fill=color_normal, tags='vis')
def step():
refresh()
repaint()
def clear():
for i in range(field_height):
for j in range(field_width):
canvas.itemconfig(cell_matrix[i][j], fill=color_hidden, tags=('hid', '0'))
def refresh():
for i in range(field_height):
for j in range(field_width):
k = 0
for i_shift in range(-1, 2):
for j_shift in range(-1, 2):
try:
if canvas.gettags(cell_matrix[i + i_shift][j + j_shift])[0] == 'vis' and (
i_shift != 0 or j_shift != 0):
k += 1
except IndexError:
k += 0
current_tag = canvas.gettags(cell_matrix[i][j])[0]
if k == 3:
canvas.itemconfig(cell_matrix[i][j], tags=(current_tag, 'to_vis'))
elif k <= 1 or k >= 4:
canvas.itemconfig(cell_matrix[i][j], tags=(current_tag, 'to_hid'))
elif k == 2 and canvas.gettags(cell_matrix[i][j])[0] == 'vis':
canvas.itemconfig(cell_matrix[i][j], tags=(current_tag, 'to_vis'))
def repaint():
for i in range(field_height):
for j in range(field_width):
if canvas.gettags(cell_matrix[i][j])[1] == 'to_vis':
canvas.itemconfig(cell_matrix[i][j], fill=color_normal, tags=('vis', '0'))
elif canvas.gettags(cell_matrix[i][j])[1] == 'to_hid':
canvas.itemconfig(cell_matrix[i][j], fill=color_hidden, tags=('hid', '0'))
def color_win():
def configParam():
global color_outline, color_hidden, color_normal
try:
color_hidden = entry_ch.get()
except TclError:
color_hidden = 'white'
canvas.config(bg='white')
canvas.config(bg=entry_ch.get())
color_normal = entry_cn.get()
if var.get() == 0:
color_outline = None
elif var.get() == 1:
color_outline = entry_co.get()
color_w = Toplevel()
color_w.geometry('300x150')
frame_co = Frame(color_w)
frame_co.pack(side=TOP, pady=20, anchor=W)
frame_cn = Frame(color_w)
frame_cn.pack(side=TOP, pady=3, anchor=W)
frame_ch = Frame(color_w)
frame_ch.pack(side=TOP, pady=3, anchor=W)
frame_rb = Frame(color_w)
frame_rb.pack(anchor=W, side=LEFT, pady=3)
frame_bc = Frame(color_w)
frame_bc.pack(side=TOP, pady=15)
lable_co = Label(frame_co, text='Outline color: ', width=15)
entry_co = Entry(frame_co, width=15)
lable_co.pack(side=LEFT, padx=3)
entry_co.pack(side=LEFT, padx=3)
lable_cn = Label(frame_cn, text='Cell color: ', width=8)
entry_cn = Entry(frame_cn, width=15)
lable_cn.pack(side=LEFT, padx=3)
entry_cn.pack(side=LEFT, padx=3)
lable_ch = Label(frame_ch, text='Background color: ', width=15)
entry_ch = Entry(frame_ch, width=15)
lable_ch.pack(side=LEFT, padx=3)
entry_ch.pack(side=LEFT, padx=3)
var = IntVar()
var.set(0)
rb_off = Radiobutton(frame_rb, text='OFF', indicatoron=0, variable=var, value=0)
rb_on = Radiobutton(frame_rb, text='ON', indicatoron=0, variable=var, value=1)
rb_off.pack(side=TOP, pady=3)
rb_on.pack(side=TOP, pady=3)
b_config = Button(frame_bc, text='Config', width=5, command=configParam)
b_config.pack(anchor=CENTER)
root = Tk()
# Ширина и высотв окна
win_height = 380
win_width = 360
config_win = '{0}x{1}'.format(win_width, win_height + 50)
root.geometry(config_win)
root.title('Life')
# размер клетки
cell_size = 10
color_normal = 'green'
color_hidden = 'white'
color_outline = 'black'
# поле, где будем рисовать
canvas = Canvas(root, height=win_height, width=win_width, bg='white')
canvas.pack(side=TOP)
# Размеры поля в клетках
field_width = win_width // cell_size
field_height = win_height // cell_size
# Матрица для клеток
cell_matrix = [[0 for i in range(field_width)] for j in range(field_height)]
for i in range(field_height):
for j in range(field_width):
square = canvas.create_rectangle(cell_size * i, cell_size * j, cell_size + cell_size * i,
cell_size + cell_size * j,
fill=color_normal, outline=color_outline)
canvas.itemconfig(square, fill=color_hidden, tags=('hid', '0'))
cell_matrix[i].pop(0)
cell_matrix[i].append(square)
fict_square = canvas.create_rectangle(-0.1, -0.1, 0, 0, fill=color_hidden, tags=('hid', '0'))
mainmenu = Menu(root)
root.config(menu=mainmenu)
mainmenu.add_command(label='Color ', command=color_win)
frame_b = Frame(root)
btn1 = Button(frame_b, text='Step', command=step)
btn2 = Button(frame_b, text='Clear', command=clear)
btn1.pack(side='left')
btn2.pack(side='right')
frame_b.pack(anchor=E, pady=1)
frame_steps = Frame(root)
frame_steps.pack(anchor=CENTER, pady=1)
lab_steps = Label(frame_steps, text='Steps', width=3)
lab_steps.pack(side=LEFT, padx=2)
ent_steps = Entry(frame_steps, width=3)
ent_steps.pack(side=LEFT, padx=2)
bt_steps = Button(frame_steps, text='Go!', width=3, command=step_while)
bt_steps.pack(side=LEFT, padx=2)
canvas.bind('<B1-Motion>', draw_a)
canvas.bind('<ButtonPress>', draw_a)
root.mainloop()