from Tkinter import Tk, Canvas, Button, Frame, BOTH, NORMAL, HIDDEN # root = Tk() # win_width = 350 win_height = 370 config_string = "{0}x{1}".format(win_width, win_height + 32) # geometry() , # '350x370', root.geometry(config_string) # , cell_size = 20 # , Canvas, # , # root canvas = Canvas(root, height=win_height) # , show() canvas.pack(fill=BOTH) # field_height = win_height / a field_width = win_width / a # , , cell_matrix = [] for i in xrange(field_height): for j in xrange(field_width): # square = canvas.create_rectangle(2 + cell_size*j, 2 + cell_size*i, cell_size + cell_size*j - 2, cell_size + cell_size*i - 2, fill="green") canvas.itemconfig(square, state=HIDDEN, tags=('hid','0')) # cell_matrix.append(square) # , fict_square = canvas.create_rectangle(0,0,0,0, state=HIDDEN, tags=('hid','0')) cell_matrix.append(fict_square) # , Canvas, # frame = Frame(root) btn1 = Button(frame, text='Eval', command = step) btn2 = Button(frame, text='Clear', command = clear) # btn1.pack(side='left') btn2.pack(side='right') # frame.pack(side='bottom') # canvas draw_a canvas.bind('', draw_a) # , c root.mainloop()
# def refresh(): for i in xrange(field_height): for j in xrange(field_width): k = 0 # for i_shift in xrange(-1, 2): for j_shift in xrange(-1, 2): if (canvas.gettags(cell_matrix[addr(i + i_shift, j + j_shift)])[0] == 'vis' and (i_shift != 0 or j_shift != 0)): k += 1 current_tag = canvas.gettags(cell_matrix[addr(i, j)])[0] # if(k == 3): canvas.itemconfig(cell_matrix[addr(i, j)], tags=(current_tag, 'to_vis')) # nota bene, "" if(k = 4): canvas.itemconfig(cell_matrix[addr(i, j)], tags=(current_tag, 'to_hid')) if(k == 2 and canvas.gettags(sm[addr(i, j)])[0] == 'vis'): canvas.itemconfig(cell_matrix[addr(i, j)], tags=(current_tag, 'to_vis')) # def repaint(): for i in xrange(field_height): for j in xrange(field_width): if (canvas.gettags(sm[addr(i, j)])[1] == 'to_hid'): canvas.itemconfig(sm[addr(i, j)], state=HIDDEN, tags=('hid','0')) if (canvas.gettags(sm[addr(i, j)])[1] == 'to_vis'): canvas.itemconfig(sm[addr(i, j)], state=NORMAL, tags=('vis','0')) # : def step(): refresh() repaint()
# def draw_a(e): ii = (ey - 3)/cell_size jj = (ex - 3)/cell_size # canvas.itemconfig(cell_matrix[addr(ii, jj)], state=NORMAL, tags='vis') # def addr(ii,jj): if(ii < 0 or jj < 0 or ii >= field_height or jj >= field_width): # return len(cell_matrix) - 1 else: return ii*(win_width/a) + jj
Source: https://habr.com/ru/post/228379/
All Articles