1 | importšmath |
---|
2 | importštkColorChooser |
---|
3 | fromšTkinteršimportš* |
---|
4 | |
---|
5 | fromšStatešimportš* |
---|
6 | fromšAutomatašimportš* |
---|
7 | |
---|
8 | |
---|
9 | classšHandlers(object): |
---|
10 | š š š š |
---|
11 | š š š š defš__init__(self,šcell_size=8,šline_width=1š,delay=10,šoffset_x=0,šoffset_y=0):# cell_size is size of cell, including line width, if there is it |
---|
12 | š š š š š š š š self.cell_size =šcell_size |
---|
13 | š š š š š š š š self.line_width =šline_width |
---|
14 | š š š š š š š š self.delay =šdelay |
---|
15 | š š š š š š š š self.offset_x =šoffset_x |
---|
16 | š š š š š š š š self.offset_y =šoffset_y |
---|
17 | š š š š š š š š self.after_id =š0 |
---|
18 | š š š š š š š š self.mouse_offset_x =š0 |
---|
19 | š š š š š š š š self.mouse_offset_y =š0 |
---|
20 | š š š š š š š š self.mouse_zoom =š0 |
---|
21 | š š š š š š š š self.zoom_divisor =š1 |
---|
22 | š š š š š š š š self.selected_state =šNone |
---|
23 | š š š š š š š š self.is_started =šFalse |
---|
24 | š š š š š š š š self.char =šNone |
---|
25 | š š š š š š š š self.keys =šdict() |
---|
26 | š š š š š š š š self.draw() |
---|
27 | š š š š defšstart(self): |
---|
28 | š š š š š š š š ifšnotšself.is_started: |
---|
29 | š š š š š š š š š š š š self.is_started =šTrue |
---|
30 | š š š š š š š š š š š š self.repeat() |
---|
31 | š š š š defšrepeat(self): |
---|
32 | š š š š š š š š self.next_step() |
---|
33 | š š š š š š š š self.after_id =šcanvas.after(self.delay,šself.repeat) |
---|
34 | š š š š |
---|
35 | š š š š defšstop(self): |
---|
36 | š š š š š š š š canvas.after_cancel(self.after_id) |
---|
37 | š š š š š š š š self.is_started =šFalse |
---|
38 | š š š š |
---|
39 | š š š š defšnext_step(self): |
---|
40 | š š š š š š š š self.draw_cell(automata.next_step()) |
---|
41 | š š š š |
---|
42 | š š š š defšsave_file(self): |
---|
43 | š š š š š š š š pass |
---|
44 | š š š š |
---|
45 | š š š š defšopen_file(self): |
---|
46 | š š š š š š š š pass |
---|
47 | š š š š |
---|
48 | š š š š defšshow_help_window(self): |
---|
49 | š š š š š š š š pass |
---|
50 | š š š š |
---|
51 | š š š š defšhide_help_window(self): |
---|
52 | š š š š š š š š pass |
---|
53 | š š š š |
---|
54 | š š š š defšzoom_in(self,šzoom_rate=1): |
---|
55 | š š š š š š š š ifšself.cell_size <š50: |
---|
56 | š š š š š š š š š š š š self.cell_size =šself.cell_size +šzoom_rate |
---|
57 | š š š š š š š š š š š š self.draw() |
---|
58 | š š š š |
---|
59 | š š š š defšzoom_out(self,šzoom_rate=1): |
---|
60 | š š š š š š š š ifšself.cell_size >š1: |
---|
61 | š š š š š š š š š š š š self.cell_size =šself.cell_size -šzoom_rate |
---|
62 | š š š š š š š š š š š š self.draw() |
---|
63 | š š š š |
---|
64 | š š š š defšslower(self,šspeed_rate=1): |
---|
65 | š š š š š š š š self.delay =šself.delay +šspeed_rate |
---|
66 | š š š š |
---|
67 | š š š š defšfaster(self,šspeed_rate=1): |
---|
68 | š š š š š š š š ifšself.delay >šspeed_rate: |
---|
69 | š š š š š š š š š š š š self.delay =šself.delay -šspeed_rate |
---|
70 | š š š š š š š š else: |
---|
71 | š š š š š š š š š š š š self.delay =š0 |
---|
72 | š š š š |
---|
73 | š š š š defšchange_size(self,šdx,šdy,šposition=0): |
---|
74 | š š š š š š š š ifšposition <š9: |
---|
75 | š š š š š š š š š š š š ifšposition ==š0šoršposition ==š3šoršposition ==š6: |
---|
76 | š š š š š š š š š š š š š š š š automata.change_size(dx,š3) |
---|
77 | š š š š š š š š š š š š elifšposition ==š1šoršposition ==š4šoršposition ==š7: |
---|
78 | š š š š š š š š š š š š š š š š automata.change_size(dx /š2,š3) |
---|
79 | š š š š š š š š š š š š š š š š automata.change_size(dx -šdx /š2,š1) |
---|
80 | š š š š š š š š š š š š else: |
---|
81 | š š š š š š š š š š š š š š š š automata.change_size(dx,š1) |
---|
82 | š š š š š š š š š š š š ifšposition ==š0šoršposition ==š1šoršposition ==š2: |
---|
83 | š š š š š š š š š š š š š š š š automata.change_size(dy,š0) |
---|
84 | š š š š š š š š š š š š elifšposition ==š3šoršposition ==š4šoršposition ==š5: |
---|
85 | š š š š š š š š š š š š š š š š automata.change_size(dy /š2,š0) |
---|
86 | š š š š š š š š š š š š š š š š automata.change_size(dy -šdy /š2,š2) |
---|
87 | š š š š š š š š š š š š else: |
---|
88 | š š š š š š š š š š š š š š š š automata.change_size(dy,š2) |
---|
89 | š š š š š š š š š š š š self.draw() |
---|
90 | š š š š |
---|
91 | š š š š defšdraw_cell(self,šcells): |
---|
92 | š š š š š š š š foršrow,šcol inšcells: |
---|
93 | š š š š š š š š š š š š index =šautomata.symbols[automata.field[row][col]] |
---|
94 | š š š š š š š š š š š š color =šautomata.states[index].color |
---|
95 | š š š š š š š š š š š š canvas.itemconfig(self.cells[row][col],šfill=color) |
---|
96 | š š š š |
---|
97 | š š š š defšdraw(self): |
---|
98 | š š š š š š š š canvas.delete("all") |
---|
99 | š š š š š š š š self.cells =š[] |
---|
100 | š š š š š š š š |
---|
101 | š š š š š š š š shift =šself.cell_size +šself.line_width |
---|
102 | š š š š š š š š š š š š š š š š |
---|
103 | š š š š š š š š left =š-self.offset_x +šself.line_width |
---|
104 | š š š š š š š š top =š-self.offset_y +šself.line_width |
---|
105 | š š š š š š š š foršrow inšrange(automata.height): |
---|
106 | š š š š š š š š š š š š self.cells.append([]) |
---|
107 | š š š š š š š š š š š š foršcol inšrange(automata.width): |
---|
108 | š š š š š š š š š š š š š š š š index =šautomata.symbols[automata.field[row][col]] |
---|
109 | š š š š š š š š š š š š š š š š color =šautomata.states[index].color |
---|
110 | š š š š š š š š š š š š š š š š cell =šcanvas.create_rectangle(left +šcol *šshift, |
---|
111 | š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š top +šrow *šshift, |
---|
112 | š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š left +šcol *šshift +šself.cell_size, |
---|
113 | š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š top +šrow *šshift +šself.cell_size, |
---|
114 | š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š š fill=color,šoutline="",štag="cell") |
---|
115 | š š š š š š š š š š š š š š š š self.cells[row].append(cell) |
---|
116 | š š š š |
---|
117 | š š š š defšdraw_line(self,šx1,šy1,šx2,šy2,šorder=1): |
---|
118 | š š š š š š š š answer =š[] |
---|
119 | š š š š š š š š ifšabs(x1 -šx2)š>šabs(y1 -šy2): |
---|
120 | š š š š š š š š š š š š dx =šx2 -šx1 |
---|
121 | š š š š š š š š š š š š abs_dx =šabs(dx) |
---|
122 | š š š š š š š š š š š š dy =šfloat(y2 -šy1) |
---|
123 | š š š š š š š š š š š š whilešx1 !=šx2: |
---|
124 | š š š š š š š š š š š š š š š š x1 =šx1 +šdx /šabs_dx |
---|
125 | š š š š š š š š š š š š š š š š y1 =šy1 +šdy /šabs_dx |
---|
126 | š š š š š š š š š š š š š š š š answer.append((self.rounding(y1),šx1)) |
---|
127 | š š š š š š š š else: |
---|
128 | š š š š š š š š š š š š dx =šfloat(x2 -šx1) |
---|
129 | š š š š š š š š š š š š dy =šy2 -šy1 |
---|
130 | š š š š š š š š š š š š abs_dy =šabs(dy) |
---|
131 | š š š š š š š š š š š š whilešy1 !=šy2: |
---|
132 | š š š š š š š š š š š š š š š š x1 =šx1 +šdx /šabs_dy |
---|
133 | š š š š š š š š š š š š š š š š y1 =šy1 +šdy /šabs_dy |
---|
134 | š š š š š š š š š š š š š š š š answer.append((y1,šself.rounding(x1))) |
---|
135 | š š š š š š š š self.new_state(answer,šorder) |
---|
136 | š š š š |
---|
137 | š š š š defšrounding(self,šnum): |
---|
138 | š š š š š š š š returnšint(num +šmath.copysign(0.5,šnum)) |
---|
139 | š š š š |
---|
140 | š š š š defšnew_state(self,šcells,šorder=1): |
---|
141 | š š š š š š š š num_states =šlen(automata.states) |
---|
142 | š š š š š š š š changed_cells =š[] |
---|
143 | š š š š š š š š foršrow,šcol inšcells: |
---|
144 | š š š š š š š š š š š š ifšcol >=š0šandšrow >=š0: |
---|
145 | š š š š š š š š š š š š š š š š try: |
---|
146 | š š š š š š š š š š š š š š š š š š š š index =š(automata.symbols[automata.field[row][col]]š+šnum_states +šorder)š%šnum_states |
---|
147 | š š š š š š š š š š š š š š š š š š š š ifšself.char !=šNonešandšself.keys.has_key(self.char): |
---|
148 | š š š š š š š š š š š š š š š š š š š š š š š š index =šself.keys[self.char] |
---|
149 | š š š š š š š š š š š š š š š š š š š š automata.field[row][col]š=šautomata.states[index].symbol |
---|
150 | š š š š š š š š š š š š š š š š š š š š changed_cells.append((row,šcol)) |
---|
151 | š š š š š š š š š š š š š š š š except: |
---|
152 | š š š š š š š š š š š š š š š š š š š š pass |
---|
153 | š š š š š š š š self.draw_cell(changed_cells) |
---|
154 | š š š š |
---|
155 | š š š š defšpress1(self,ševent):# drawer |
---|
156 | š š š š š š š š self.col1 =ševent.x /š(self.cell_size +šself.line_width)š-šself.offset_x |
---|
157 | š š š š š š š š self.row1 =ševent.y /š(self.cell_size +šself.line_width)š-šself.offset_y |
---|
158 | š š š š š š š š ifšself.col1 >=š0šandšself.row1 >=š0: |
---|
159 | š š š š š š š š š š š š self.new_state([(self.row1,šself.col1)]) |
---|
160 | š š š š š š š š |
---|
161 | š š š š defšmotion1(self,ševent):# drawer |
---|
162 | š š š š š š š š col =š(event.x -šself.offset_x *š(self.cell_size +šself.line_width))š/š(self.cell_size +šself.line_width) |
---|
163 | š š š š š š š š row =š(event.y -šself.offset_y *š(self.cell_size +šself.line_width))š/š(self.cell_size +šself.line_width) |
---|
164 | š š š š š š š š ifšnotš(self.col1 ==šcol andšself.row1 ==šrow): |
---|
165 | š š š š š š š š š š š š ifšabs(self.col1 -šcol)š<=š1šandšabs(self.row1 -šrow)š<=š1: |
---|
166 | š š š š š š š š š š š š š š š š self.new_state([(row,šcol)]) |
---|
167 | š š š š š š š š š š š š else: |
---|
168 | š š š š š š š š š š š š š š š š self.draw_line(self.col1,šself.row1,šcol,šrow) |
---|
169 | š š š š š š š š š š š š self.col1 =šcol |
---|
170 | š š š š š š š š š š š š self.row1 =šrow |
---|
171 | š š š š |
---|
172 | š š š š defšpress3(self,ševent):# drawer |
---|
173 | š š š š š š š š self.col3 =ševent.x /š(self.cell_size +šself.line_width)š-šself.offset_x |
---|
174 | š š š š š š š š self.row3 =ševent.y /š(self.cell_size +šself.line_width)š-šself.offset_y |
---|
175 | š š š š š š š š ifšself.col1 >=š0šandšself.row1 >=š0: |
---|
176 | š š š š š š š š š š š š self.new_state([(self.row3,šself.col3)],š-1) |
---|
177 | š š š š |
---|
178 | š š š š defšmotion3(self,ševent):# drawer |
---|
179 | š š š š š š š š col =š(event.x -šself.offset_x *š(self.cell_size +šself.line_width))š/š(self.cell_size +šself.line_width) |
---|
180 | š š š š š š š š row =š(event.y -šself.offset_y *š(self.cell_size +šself.line_width))š/š(self.cell_size +šself.line_width) |
---|
181 | š š š š š š š š ifšnotš(self.col3 ==šcol andšself.row3 ==šrow): |
---|
182 | š š š š š š š š š š š š ifšabs(self.col3 -šcol)š<=š1šandšabs(self.row3 -šrow)š<=š1: |
---|
183 | š š š š š š š š š š š š š š š š self.new_state([(row,šcol)],š-1) |
---|
184 | š š š š š š š š š š š š else: |
---|
185 | š š š š š š š š š š š š š š š š self.draw_line(self.col3,šself.row3,šcol,šrow,š-1) |
---|
186 | š š š š š š š š š š š š self.col3 =šcol |
---|
187 | š š š š š š š š š š š š self.row3 =šrow |
---|
188 | š š š š |
---|
189 | š š š š defšpress_key(self,ševent):# drawer |
---|
190 | š š š š š š š š self.char =ševent.char |
---|
191 | š š š š |
---|
192 | š š š š defšrelease_key(self,ševent):# drawer |
---|
193 | š š š š š š š š self.char =šNone |
---|
194 | š š š š |
---|
195 | š š š š defšpress1_ctrl(self,ševent):# change_offset (B1+ctrl) |
---|
196 | š š š š š š š š canvas.focus_set() |
---|
197 | š š š š š š š š self.mouse_offset_x =ševent.x |
---|
198 | š š š š š š š š self.mouse_offset_y =ševent.y |
---|
199 | š š š š |
---|
200 | š š š š defšmotion1_ctrl(self,ševent):# change_offset (B1+ctrl) |
---|
201 | š š š š š š š š self.offset_x =š(event.x -šself.mouse_offset_x)š/š(self.cell_size +šself.line_width) |
---|
202 | š š š š š š š š self.offset_y =š(event.y -šself.mouse_offset_y)š/š(self.cell_size +šself.line_width) |
---|
203 | š š š š š š š š self.mouse_offset_x =ševent.x |
---|
204 | š š š š š š š š self.mouse_offset_y =ševent.y |
---|
205 | š š š š š š š š self.draw() |
---|
206 | š š š š |
---|
207 | š š š š defšpress13(self,ševent):# zoom |
---|
208 | š š š š š š š š canvas.focus_set() |
---|
209 | š š š š š š š š self.mouse_zoom =ševent.y |
---|
210 | š š š š |
---|
211 | š š š š defšmotion13(self,ševent):# zoom |
---|
212 | š š š š š š š š delta =š(event.y -šself.mouse_zoom)š/šself.zoom_divisor |
---|
213 | š š š š š š š š self.cell_size =šself.cell_size +šdelta |
---|
214 | š š š š š š š š ifšself.cell_size >š50: |
---|
215 | š š š š š š š š š š š š self.cell_size =š50 |
---|
216 | š š š š š š š š ifšself.cell_size <š1: |
---|
217 | š š š š š š š š š š š š self.cell_size =š1 |
---|
218 | š š š š š š š š self.mouse_zoom =ševent.y |
---|
219 | š š š š š š š š self.draw() |
---|
220 | š š š š |
---|
221 | š š š š defšto_top(self):# replace choosen state to top |
---|
222 | š š š š š š š š selected =šself.selected_state |
---|
223 | š š š š š š š š ifšselected !=šNone: |
---|
224 | š š š š š š š š š š š š index =šselected |
---|
225 | š š š š š š š š š š š š state =šautomata.states[index] |
---|
226 | š š š š š š š š š š š š printšstate |
---|
227 | š š š š š š š š š š š š delšautomata.states[index] |
---|
228 | š š š š š š š š š š š š automata.states.insert(0,šstate) |
---|
229 | š š š š š š š š š š š š self.selected_state =š0 |
---|
230 | š š š š š š š š š š š š self.refresh_list() |
---|
231 | š š š š |
---|
232 | š š š š defšto_bottom(self):# replace choosen state to botton |
---|
233 | š š š š š š š š selected =šself.selected_state |
---|
234 | š š š š š š š š ifšselected !=šNone: |
---|
235 | š š š š š š š š š š š š index =šselected |
---|
236 | š š š š š š š š š š š š state =šautomata.states[index] |
---|
237 | š š š š š š š š š š š š printšstate |
---|
238 | š š š š š š š š š š š š delšautomata.states[index] |
---|
239 | š š š š š š š š š š š š automata.states.append(state) |
---|
240 | š š š š š š š š š š š š self.selected_state =šlen(automata.states)š-š1 |
---|
241 | š š š š š š š š š š š š self.refresh_list() |
---|
242 | š š š š |
---|
243 | š š š š defšupwards(self): |
---|
244 | š š š š š š š š selected =šself.selected_state |
---|
245 | š š š š š š š š ifšselected !=šNone: |
---|
246 | š š š š š š š š š š š š index =šselected |
---|
247 | š š š š š š š š š š š š ifšindex >š0: |
---|
248 | š š š š š š š š š š š š š š š š state =šautomata.states[index] |
---|
249 | š š š š š š š š š š š š š š š š printšstate |
---|
250 | š š š š š š š š š š š š š š š š delšautomata.states[index] |
---|
251 | š š š š š š š š š š š š š š š š automata.states.insert(index -š1,šstate) |
---|
252 | š š š š š š š š š š š š š š š š self.selected_state =šindex -š1 |
---|
253 | š š š š š š š š š š š š š š š š self.refresh_list() |
---|
254 | š š š š |
---|
255 | š š š š defšdownwards(self): |
---|
256 | š š š š š š š š selected =šself.selected_state |
---|
257 | š š š š š š š š ifšselected !=šNone: |
---|
258 | š š š š š š š š š š š š index =šselected |
---|
259 | š š š š š š š š š š š š ifšindex <šstate_list.size()š-š1: |
---|
260 | š š š š š š š š š š š š š š š š state =šautomata.states[index] |
---|
261 | š š š š š š š š š š š š š š š š printšstate |
---|
262 | š š š š š š š š š š š š š š š š delšautomata.states[index] |
---|
263 | š š š š š š š š š š š š š š š š automata.states.insert(index +š1,šstate) |
---|
264 | š š š š š š š š š š š š š š š š self.selected_state =šindex +š1 |
---|
265 | š š š š š š š š š š š š š š š š self.refresh_list() |
---|
266 | š š š š |
---|
267 | š š š š defšdelete_state(self):# delete choosen state |
---|
268 | š š š š š š š š selected =šself.selected_state |
---|
269 | š š š š š š š š ifšselected !=šNone: |
---|
270 | š š š š š š š š š š š š index =šselected |
---|
271 | š š š š š š š š š š š š printšautomata.states[index] |
---|
272 | š š š š š š š š š š š š delšautomata.symbols[automata.states[index].symbol] |
---|
273 | š š š š š š š š š š š š forškey inšself.keys.keys(): |
---|
274 | š š š š š š š š š š š š š š š š ifšself.keys[key]š==šindex: |
---|
275 | š š š š š š š š š š š š š š š š š š š š delšself.keys[key] |
---|
276 | š š š š š š š š š š š š delšautomata.states[index] |
---|
277 | š š š š š š š š š š š š ifšindex inšautomata.states: |
---|
278 | š š š š š š š š š š š š š š š š self.selected_state =šindex |
---|
279 | š š š š š š š š š š š š elifšlen(automata.states): |
---|
280 | š š š š š š š š š š š š š š š š self.selected_state =šlen(automata.states)š-š1 |
---|
281 | š š š š š š š š š š š š else: |
---|
282 | š š š š š š š š š š š š š š š š self.selected_state =šNone |
---|
283 | š š š š š š š š š š š š self.refresh_list() |
---|
284 | š š š š š š š š š š š š self.select_item(self.selected_state) |
---|
285 | š š š š |
---|
286 | š š š š defšadd(self):# add new state |
---|
287 | š š š š š š š š name =šstate_name.get() |
---|
288 | š š š š š š š š symbol =šstate_symbol.get() |
---|
289 | š š š š š š š š key =šstate_key.get().lower() |
---|
290 | š š š š š š š š color =šstate_color.cget("bg") |
---|
291 | š š š š š š š š nums =š[] |
---|
292 | š š š š š š š š forši,švalue inšenumerate(ckeckbox_nums): |
---|
293 | š š š š š š š š š š š š š š š š ifšvalue.get()š==š1: |
---|
294 | š š š š š š š š š š š š š š š š š š š š nums.append(i) |
---|
295 | š š š š š š š š ifšself.keys.has_key(key): |
---|
296 | š š š š š š š š š š š š error.config(text="State with such key has already existed") |
---|
297 | š š š š š š š š š š š š state_key.focus() |
---|
298 | š š š š š š š š elifšlen(key)š!=š1: |
---|
299 | š š š š š š š š š š š š error.config(text="Bad key for state") |
---|
300 | š š š š š š š š š š š š state_key.focus() |
---|
301 | š š š š š š š š elifšautomata.symbols.has_key(symbol): |
---|
302 | š š š š š š š š š š š š error.config(text="State with such symbol has already existed") |
---|
303 | š š š š š š š š š š š š state_symbol.focus() |
---|
304 | š š š š š š š š elifšlen(symbol)š!=š1: |
---|
305 | š š š š š š š š š š š š error.config(text="Bad symbol for state") |
---|
306 | š š š š š š š š š š š š state_symbol.focus() |
---|
307 | š š š š š š š š else: |
---|
308 | š š š š š š š š š š š š state =šState(name,šsymbol,šcolor,šnums) |
---|
309 | š š š š š š š š š š š š automata.states.append(state) |
---|
310 | š š š š š š š š š š š š automata.symbols[symbol]š=šlen(automata.states)š-š1 |
---|
311 | š š š š š š š š š š š š self.keys[key]š=šlen(automata.states)š-š1 |
---|
312 | š š š š š š š š š š š š error.config(text="") |
---|
313 | š š š š š š š š š š š š self.selected_state =šlen(automata.states)š-š1 |
---|
314 | š š š š š š š š š š š š self.refresh_list() |
---|
315 | š š š š š š š š š š š š self.select_item(self.selected_state) |
---|
316 | š š š š |
---|
317 | š š š š defšchange(self):# change chosen state |
---|
318 | š š š š š š š š selected =šself.selected_state |
---|
319 | š š š š š š š š ifšselected !=šNone: |
---|
320 | š š š š š š š š š š š š index =šselected |
---|
321 | š š š š š š š š š š š š name =šstate_name.get() |
---|
322 | š š š š š š š š š š š š symbol =šstate_symbol.get() |
---|
323 | š š š š š š š š š š š š key =šstate_key.get().lower() |
---|
324 | š š š š š š š š š š š š color =šstate_color.cget("bg") |
---|
325 | š š š š š š š š š š š š nums =š[] |
---|
326 | š š š š š š š š š š š š forši,švalue inšenumerate(ckeckbox_nums): |
---|
327 | š š š š š š š š š š š š š š š š printši,švalue.get() |
---|
328 | š š š š š š š š š š š š š š š š ifšvalue.get()š==š1: |
---|
329 | š š š š š š š š š š š š š š š š š š š š nums.append(i) |
---|
330 | š š š š š š š š š š š š printšnums |
---|
331 | š š š š š š š š š š š š ifšself.keys.has_key(key)šandšself.keys[key]š!=šindex: |
---|
332 | š š š š š š š š š š š š š š š š error.config(text="State with such key has already existed") |
---|
333 | š š š š š š š š š š š š š š š š state_key.focus() |
---|
334 | š š š š š š š š š š š š elifšlen(key)š!=š1: |
---|
335 | š š š š š š š š š š š š š š š š error.config(text="Bad key for state") |
---|
336 | š š š š š š š š š š š š š š š š state_key.focus() |
---|
337 | š š š š š š š š š š š š elifšautomata.symbols.has_key(symbol)š andšautomata.symbols[symbol]š!=šindex: |
---|
338 | š š š š š š š š š š š š š š š š error.config(text="State with such symbol has already existed") |
---|
339 | š š š š š š š š š š š š š š š š state_symbol.focus() |
---|
340 | š š š š š š š š š š š š elifšlen(symbol)š!=š1: |
---|
341 | š š š š š š š š š š š š š š š š error.config(text="Bad symbol for state") |
---|
342 | š š š š š š š š š š š š š š š š state_symbol.focus() |
---|
343 | š š š š š š š š š š š š else: |
---|
344 | š š š š š š š š š š š š š š š š state =šState(name,šsymbol,šcolor,šnums) |
---|
345 | š š š š š š š š š š š š š š š š automata.states[index]š=šstate |
---|
346 | š š š š š š š š š š š š š š š š automata.symbols[symbol]š=šindex |
---|
347 | š š š š š š š š š š š š š š š š self.keys[key]š=šindex |
---|
348 | š š š š š š š š š š š š š š š š error.config(text="") |
---|
349 | š š š š š š š š š š š š š š š š self.refresh_list() |
---|
350 | š š š š |
---|
351 | š š š š defšshow_size_window(self): |
---|
352 | š š š š š š š š size_window.deiconify() |
---|
353 | š š š š |
---|
354 | š š š š defšhide_size_window(self): |
---|
355 | š š š š š š š š size_window.withdraw() |
---|
356 | š š š š |
---|
357 | š š š š defšshow_automata_window(self): |
---|
358 | š š š š š š š š automata_window.deiconify() |
---|
359 | š š š š |
---|
360 | š š š š defšhide_automata_window(self): |
---|
361 | š š š š š š š š automata_window.withdraw() |
---|
362 | š š š š |
---|
363 | š š š š defšrefresh_list(self): |
---|
364 | š š š š š š š š state_list.delete(0,š"end") |
---|
365 | š š š š š š š š foršstate inšautomata.states: |
---|
366 | š š š š š š š š š š š š state_list.insert("end",šstate) |
---|
367 | š š š š š š š š ifšself.selected_state !=šNone: |
---|
368 | š š š š š š š š š š š š state_list.selection_set(self.selected_state) |
---|
369 | š š š š |
---|
370 | š š š š defšselect_item(self,šindex): |
---|
371 | š š š š š š š š ifšindex !=šNone: |
---|
372 | š š š š š š š š š š š š state =šautomata.states[index] |
---|
373 | š š š š š š š š š š š š state_name.delete(0,š"end") |
---|
374 | š š š š š š š š š š š š state_name.insert(0,šstate.name) |
---|
375 | š š š š š š š š š š š š state_symbol.delete(0,š"end") |
---|
376 | š š š š š š š š š š š š state_symbol.insert(0,šstate.symbol) |
---|
377 | š š š š š š š š š š š š forškey inšself.keys.keys(): |
---|
378 | š š š š š š š š š š š š š š š š ifšself.keys[key]š==šindex: |
---|
379 | š š š š š š š š š š š š š š š š š š š š state_key.delete(0,š"end") |
---|
380 | š š š š š š š š š š š š š š š š š š š š state_key.insert(0,škey) |
---|
381 | š š š š š š š š š š š š state_color.config(bg=state.color) |
---|
382 | š š š š š š š š š š š š forši inšrange(9): |
---|
383 | š š š š š š š š š š š š š š š š ckeckbox_nums[i].set(i inšstate.nums) |
---|
384 | š š š š š š š š else: |
---|
385 | š š š š š š š š š š š š state_name.delete(0,š"end") |
---|
386 | š š š š š š š š š š š š state_symbol.delete(0,š"end") |
---|
387 | š š š š š š š š š š š š state_key.delete(0,š"end") |
---|
388 | š š š š š š š š š š š š state_color.config(bg="white") |
---|
389 | š š š š š š š š š š š š forši inšrange(9): |
---|
390 | š š š š š š š š š š š š š š š š ckeckbox_nums[i].set(False) |
---|
391 | š š š š |
---|
392 | š š š š defšlist_mouse_release(self,ševent): |
---|
393 | š š š š š š š š selected =šstate_list.curselection() |
---|
394 | š š š š š š š š self.selected_state =šint(selected[0]) |
---|
395 | š š š š š š š š ifšlen(selected): |
---|
396 | š š š š š š š š š š š š self.select_item(self.selected_state) |
---|
397 | š š š š defšchoose_color(self,ševent): |
---|
398 | š š š š š š š š state_color.config(bg=tkColorChooser.askcolor()[1]) |
---|
399 | |
---|
400 | |
---|
401 | root =šTk() |
---|
402 | root.title("Cyclic Cellular Automata") |
---|
403 | |
---|
404 | canvas =šCanvas(root,šbackground="grey") |
---|
405 | canvas.config(width=500,šheight=400) |
---|
406 | |
---|
407 | automata =šAutomata() |
---|
408 | handlers =šHandlers() |
---|
409 | |
---|
410 | canvas.tag_bind("cell",š"<1>",šhandlers.press1) |
---|
411 | canvas.tag_bind("cell",š"<B1-Motion>",šhandlers.motion1) |
---|
412 | canvas.tag_bind("cell",š"<3>",šhandlers.press3) |
---|
413 | canvas.tag_bind("cell",š"<B3-Motion>",šhandlers.motion3) |
---|
414 | canvas.bind_all("<KeyPress>",šhandlers.press_key) |
---|
415 | canvas.bind_all("<KeyRelease>",šhandlers.release_key) |
---|
416 | #canvas.bind("<Control-ButtonPress-1>", handlers.press1_ctrl) |
---|
417 | #canvas.bind("<Control-B1-Motion>", handlers.motion1_ctrl) |
---|
418 | #canvas.bind("<Shift-ButtonPress-1>", handlers.press13) |
---|
419 | #canvas.bind("<Shift-ButtonPress-1>", handlers.motion13) |
---|
420 | |
---|
421 | canvas.pack(fill="both",šexpand="yes") |
---|
422 | |
---|
423 | #infoPanel=Frame |
---|
424 | automata_window =šToplevel(root) |
---|
425 | automata_window.title("Automata") |
---|
426 | automata_window.withdraw() |
---|
427 | automata_window.resizable(False,šFalse) |
---|
428 | automata_window.protocol("WM_DELETE_WINDOW",šhandlers.hide_automata_window) |
---|
429 | |
---|
430 | headline =šLabel(automata_window,štext=š"Automata Panel",šfont=16) |
---|
431 | headline.pack(side="top",fill="both",šexpand="no") |
---|
432 | |
---|
433 | Label(automata_window,štext=š"State Box:").pack(side="top",šfill="x") |
---|
434 | |
---|
435 | list_frame=Frame(automata_window) |
---|
436 | scrollbar =šScrollbar(list_frame) |
---|
437 | scrollbar.pack(side="right",šfill="y") |
---|
438 | state_list=Listbox(list_frame,šyscrollcommand=scrollbar.set,šactivestyle="none",šselectmode="single") |
---|
439 | handlers.refresh_list() |
---|
440 | state_list.bind("<ButtonRelease-1>",šhandlers.list_mouse_release) |
---|
441 | state_list.pack(side="top",šfill="y") |
---|
442 | scrollbar.config(command=state_list.yview) |
---|
443 | list_frame.pack(side="top") |
---|
444 | |
---|
445 | manip_frame1 =šFrame(automata_window,špadx=10,špady=5) |
---|
446 | up =šButton(manip_frame1,štext="Up",šcommand=handlers.upwards,šwidth=10) |
---|
447 | to_top =šButton(manip_frame1,štext="To Top",šcommand=handlers.to_top,šwidth=10) |
---|
448 | up.pack(side="left",šfill="x") |
---|
449 | to_top.pack(side="right",šfill="x") |
---|
450 | manip_frame1.pack(side="top",šfill="x") |
---|
451 | |
---|
452 | manip_frame2 =šFrame(automata_window,špadx=10,špady=5) |
---|
453 | down =šButton(manip_frame2,štext="Down",šcommand=handlers.downwards,šwidth=10) |
---|
454 | to_bottom =šButton(manip_frame2,štext="To Bottom",šcommand=handlers.to_bottom,šwidth=10) |
---|
455 | down.pack(side="left",šfill="x") |
---|
456 | to_bottom.pack(side="right",šfill="x") |
---|
457 | manip_frame2.pack(side="top",šfill="x") |
---|
458 | |
---|
459 | delete =šButton(automata_window,štext="Delete",šcommand=handlers.delete_state,šwidth=10) |
---|
460 | delete.pack(side="top") |
---|
461 | |
---|
462 | |
---|
463 | information =šLabel(automata_window,štext=š"Information of State") |
---|
464 | information.pack(side="top",šfill="x") |
---|
465 | info_frame=Frame(automata_window) |
---|
466 | Label(info_frame,štext="Name").grid(row=0,šcolumn=0) |
---|
467 | state_name =šEntry(info_frame) |
---|
468 | state_name.grid(row=0,šcolumn=1) |
---|
469 | Label(info_frame,štext="Symbol").grid(row=1,šcolumn=0) |
---|
470 | state_symbol =šEntry(info_frame) |
---|
471 | state_symbol.grid(row=1,šcolumn=1) |
---|
472 | Label(info_frame,štext="Key").grid(row=2,šcolumn=0) |
---|
473 | state_key =šEntry(info_frame) |
---|
474 | state_key.grid(row=2,šcolumn=1) |
---|
475 | Label(info_frame,štext="Color").grid(row=3,šcolumn=0) |
---|
476 | state_color =šLabel(info_frame,šbackground="white",šcursor="plus") |
---|
477 | state_color.grid(row=3,šcolumn=1,šsticky="ew") |
---|
478 | state_color.bind('<1>',šhandlers.choose_color) |
---|
479 | info_frame.pack(side="top") |
---|
480 | |
---|
481 | |
---|
482 | ckeckbox_nums =š[IntVar(),šIntVar(),šIntVar(),šIntVar(),šIntVar(),š |
---|
483 | š š š š š š š š š š š š š š š š š š š š š š š š š š š š IntVar(),šIntVar(),šIntVar(),šIntVar()] |
---|
484 | |
---|
485 | condition =šLabel(automata_window,štext=š"Condition of conversion") |
---|
486 | condition.pack(side="top",šfill="x") |
---|
487 | condition_frame=Frame(automata_window) |
---|
488 | Label(condition_frame,štext="0: ").grid(row=0,šcolumn=0) |
---|
489 | c_button_0 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[0]) |
---|
490 | c_button_0.grid(row=0,šcolumn=1) |
---|
491 | Label(condition_frame,štext="1: ").grid(row=0,šcolumn=2) |
---|
492 | c_button_1 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[1]) |
---|
493 | c_button_1.grid(row=0,šcolumn=3) |
---|
494 | Label(condition_frame,štext="2: ").grid(row=0,šcolumn=4) |
---|
495 | c_button_2 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[2]) |
---|
496 | c_button_2.grid(row=0,šcolumn=5) |
---|
497 | Label(condition_frame,štext="3: ").grid(row=1,šcolumn=0) |
---|
498 | c_button_3 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[3]) |
---|
499 | c_button_3.grid(row=1,šcolumn=1) |
---|
500 | Label(condition_frame,štext="4: ").grid(row=1,šcolumn=2) |
---|
501 | c_button_4 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[4]) |
---|
502 | c_button_4.grid(row=1,šcolumn=3) |
---|
503 | Label(condition_frame,štext="5: ").grid(row=1,šcolumn=4) |
---|
504 | c_button_5 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[5]) |
---|
505 | c_button_5.grid(row=1,šcolumn=5) |
---|
506 | Label(condition_frame,štext="6: ").grid(row=2,šcolumn=0) |
---|
507 | c_button_6 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[6]) |
---|
508 | c_button_6.grid(row=2,šcolumn=1) |
---|
509 | Label(condition_frame,štext="7: ").grid(row=2,šcolumn=2) |
---|
510 | c_button_7 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[7]) |
---|
511 | c_button_7.grid(row=2,šcolumn=3) |
---|
512 | Label(condition_frame,štext="8: ").grid(row=2,šcolumn=4) |
---|
513 | c_button_8 =šCheckbutton(condition_frame,švariable=ckeckbox_nums[8]) |
---|
514 | c_button_8.grid(row=2,šcolumn=5) |
---|
515 | condition_frame.pack(side="top") |
---|
516 | |
---|
517 | |
---|
518 | add_frame =šFrame(automata_window,špadx=10,špady=5) |
---|
519 | add_state =šButton(add_frame,štext="Add",šcommand=handlers.add,šwidth=10) |
---|
520 | change_state =šButton(add_frame,štext="Change",šcommand=handlers.change,šwidth=10) |
---|
521 | add_state.pack(side="left",šfill="x") |
---|
522 | change_state.pack(side="right",šfill="x") |
---|
523 | add_frame.pack(side="top",šfill="x") |
---|
524 | |
---|
525 | error=Label(automata_window) |
---|
526 | error.pack(side="top",šfill="x") |
---|
527 | |
---|
528 | |
---|
529 | side =š0 |
---|
530 | |
---|
531 | size_window =šToplevel(root) |
---|
532 | size_window.title("") |
---|
533 | size_window.withdraw() |
---|
534 | size_window.resizable(False,šFalse) |
---|
535 | size_window.protocol("WM_DELETE_WINDOW",šhandlers.hide_size_window) |
---|
536 | Label(size_window,štext=š"Current size of window:").pack(side="top",šfill="x") |
---|
537 | size =šLabel(size_window,štext=šstr(len(automata.field))š+š" x "š+šstr(len(automata.field[0]))) |
---|
538 | size.pack(side="top",šfill="x") |
---|
539 | Label(size_window,štext=š"New size:").pack(side="top",šfill="x") |
---|
540 | new_size =šFrame(size_window) |
---|
541 | size_x =šEntry(new_size,šwidth=5) |
---|
542 | size_x.grid(row=0,šcolumn=0) |
---|
543 | Label(new_size,štext=" x ").grid(row=0,šcolumn=1) |
---|
544 | size_y =šEntry(new_size,šwidth=5) |
---|
545 | size_y.grid(row=0,šcolumn=2) |
---|
546 | new_size.pack(side="top") |
---|
547 | Label(size_window,štext=š"Expansion of window:").pack(side="top",šfill="x") |
---|
548 | expansion =šFrame(size_window) |
---|
549 | r0 =šRadiobutton(expansion,švariable=side,švalue =š0,šindicatoron=0,šwidth=2,šheight=1) |
---|
550 | r0.select() |
---|
551 | r0.grid(row=0,šcolumn=0)š š |
---|
552 | r1 =šRadiobutton(expansion,švariable=side,švalue =š1,šindicatoron=0,šwidth=2,šheight=1) |
---|
553 | r1.grid(row=0,šcolumn=1)š š |
---|
554 | r2 =šRadiobutton(expansion,švariable=side,švalue =š2,šindicatoron=0,šwidth=2,šheight=1) |
---|
555 | r2.grid(row=0,šcolumn=2)š š |
---|
556 | r3 =šRadiobutton(expansion,švariable=side,švalue =š3,šindicatoron=0,šwidth=2,šheight=1) |
---|
557 | r3.grid(row=1,šcolumn=0)š š |
---|
558 | r4 =šRadiobutton(expansion,švariable=side,švalue =š4,šindicatoron=0,šwidth=2,šheight=1) |
---|
559 | r4.grid(row=1,šcolumn=1)š š |
---|
560 | r5 =šRadiobutton(expansion,švariable=side,švalue =š5,šindicatoron=0,šwidth=2,šheight=1) |
---|
561 | r5.grid(row=1,šcolumn=2)š š |
---|
562 | r6 =šRadiobutton(expansion,švariable=side,švalue =š6,šindicatoron=0,šwidth=2,šheight=1) |
---|
563 | r6.grid(row=2,šcolumn=0)š š |
---|
564 | r7 =šRadiobutton(expansion,švariable=side,švalue =š7,šindicatoron=0,šwidth=2,šheight=1) |
---|
565 | r7.grid(row=2,šcolumn=1)š š |
---|
566 | r8 =šRadiobutton(expansion,švariable=side,švalue =š8,šindicatoron=0,šwidth=2,šheight=1) |
---|
567 | r8.grid(row=2,šcolumn=2) |
---|
568 | expansion.pack(side="top") |
---|
569 | Label(size_window).pack(side="top",šfill="x") |
---|
570 | apply_frame =šFrame(size_window,špadx=10,špady=5) |
---|
571 | apply_size =šButton(apply_frame,štext="Apply",šwidth=6) |
---|
572 | apply_size.pack(side="left",šfill="x") |
---|
573 | close_size =šButton(apply_frame,štext="Close",šcommand=handlers.hide_size_window,šwidth=6) |
---|
574 | close_size.pack(side="right",šfill="x") |
---|
575 | apply_frame.pack(side="top",šfill="x") |
---|
576 | |
---|
577 | menubar =šMenu(root) |
---|
578 | root.config(menu=menubar) |
---|
579 | |
---|
580 | menu_file =šMenu(menubar) |
---|
581 | menu_file.add_command(label="New") |
---|
582 | menu_file.add_command(label="Open...",šcommand=handlers.open_file) |
---|
583 | menu_file.add_command(label="Save...",šcommand=handlers.save_file) |
---|
584 | menu_file.add_separator() |
---|
585 | menu_file.add_command(label="Exit",šcommand=root.destroy) |
---|
586 | menubar.add_cascade(label="File",šmenu=menu_file) |
---|
587 | |
---|
588 | menu_action =šMenu(menubar) |
---|
589 | menu_action.add_command(label="Start",šcommand=handlers.start) |
---|
590 | menu_action.add_command(label="Stop",šcommand=handlers.stop) |
---|
591 | menu_action.add_command(label="Next Step",šcommand=handlers.next_step) |
---|
592 | menu_action.add_separator() |
---|
593 | menu_action.add_command(label="Increase speed",šcommand=handlers.faster) |
---|
594 | menu_action.add_command(label="Decrease speed",šcommand=handlers.slower) |
---|
595 | menu_action.add_separator() |
---|
596 | menu_action.add_command(label="Zoom In",šcommand=handlers.zoom_in) |
---|
597 | menu_action.add_command(label="Zoom Out",šcommand=handlers.zoom_out) |
---|
598 | menu_action.add_separator() |
---|
599 | menu_action.add_command(label="Clean field") |
---|
600 | menu_action.add_command(label="Fill randomly") |
---|
601 | menu_action.add_separator() |
---|
602 | menu_action.add_command(label="Change size",command=handlers.show_size_window) |
---|
603 | menubar.add_cascade(label="Action",šmenu=menu_action) |
---|
604 | |
---|
605 | menubar.add_command(label="Automata",šcommand=handlers.show_automata_window) |
---|
606 | |
---|
607 | menubar.add_command(label="Help",šcommand=handlers.show_help_window) |
---|
608 | |
---|
609 | root.mainloop() |
---|
610 | |
---|