Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/file/282ebe06d5ac/Interface.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 10:42:50 2013
Кодировка:
cca: 282ebe06d5ac Interface.py

cca

view Interface.py @ 37:282ebe06d5ac

Automated merge with ssh://kodomo.fbb.msu.ru/cca
author Ilia
date Sun, 05 Dec 2010 14:47:25 +0300
parents 992d0179053e
children 031f654a7b7d
line source
1 from Tkinter import *
3 from State import *
4 from Automata import *
7 class Handlers(object):
9 def __init__(self, cell_size=5, line_width=1 ,delay=10, offset_x=0, offset_y=0):# cell_size is size of cell, including line width, if there is it
10 self.cell_size = cell_size
11 self.line_width = line_width
12 self.delay = delay
13 self.offset_x = offset_x
14 self.offset_y = offset_y
15 self.after_id = 0
16 self.mouse_x = 0
17 self.mouse_y = 0
18 self.mouse_zoom = 0
19 self.zoom_divisor = 10
20 self.is_started = False
21 self.keys = dict()
22 self.draw()
23 def start(self):
24 if not self.is_started:
25 self.is_started = True
26 self.next_step()
27 self.after_id = canvas.after(self.delay, self.start)
29 def stop(self):
30 canvas.after_cancel(self.after_id)
31 self.is_started = False
33 def next_step(self):
34 automata.next_step()
35 self.draw()
37 def save_file(self):
38 pass
40 def open_file(self):
41 pass
43 def show_help_window(self):
44 pass
46 def hide_help_window(self):
47 pass
49 def zoom_in(self, zoom_rate=1):
50 if self.cell_size < 50:
51 self.cell_size = self.cell_size + zoom_rate
52 self.draw()
54 def zoom_out(self, zoom_rate=1):
55 if self.cell_size > 1:
56 self.cell_size = self.cell_size - zoom_rate
57 self.draw()
59 def slower(self, speed_rate=1):
60 self.delay = self.delay + speed_rate
62 def faster(self, speed_rate=1):
63 if self.delay > speed_rate:
64 self.delay = self.delay - speed_rate
65 else:
66 self.delay = 0
68 def change_size(self, dx, dy, position=0):
69 if position < 9:
70 if position == 0 or position == 3 or position == 6:
71 automata.change_size(dx, 3)
72 elif position == 1 or position == 4 or position == 7:
73 automata.change_size(dx / 2, 3)
74 automata.change_size(dx - dx / 2, 1)
75 else:
76 automata.change_size(dx, 1)
77 if position == 0 or position == 1 or position == 2:
78 automata.change_size(dy, 0)
79 elif position == 3 or position == 4 or position == 5:
80 automata.change_size(dy / 2, 0)
81 automata.change_size(dy - dy / 2, 2)
82 else:
83 automata.change_size(dy, 2)
84 self.draw()
86 def draw(self):
87 canvas.delete("all")
89 shift = self.cell_size + self.line_width
90 left = -self.offset_x
91 top = -self.offset_x
92 for row in range(automata.height + 1):
93 canvas.create_rectangle(left,
94 top + row * shift,
95 left + (automata.width) * shift + 1,
96 top + row * shift + self.line_width,
97 fill="grey", outline="")
98 for col in range(automata.width + 1):
99 canvas.create_rectangle(left + col * shift,
100 top,
101 left + col * shift + self.line_width,
102 top + (automata.height) * shift + 1,
103 fill="grey", outline="")
104 left += self.line_width
105 top += self.line_width
106 for row in range(automata.height):
107 for col in range(automata.width):
108 index = automata.symbols[automata.field[row][col]]
109 color = automata.states[index].color
110 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="")
116 def press1(self, event):# drawer
117 column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
118 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
119 index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states)
120 automata.field[row][column] = automata.states[index].symbol
121 self.draw()
123 def motion1(self, event):# drawer
124 column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
125 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
126 index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states)
127 automata.field[row][column] = automata.states[index].symbol
128 self.draw()
130 def press3(self, event):# drawer
131 column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
132 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
133 index = (automata.symbols[automata.field[row][column]] + len(automata.states) - 1) % len(automata.states)
134 automata.field[row][column] = automata.states[index].symbol
135 self.draw()
137 def motion3(self, event):# drawer
138 column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
139 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
140 index = (automata.symbols[automata.field[row][column]] + len(automata.states) - 1) % len(automata.states)
141 automata.field[row][column] = automata.states[index].symbol
142 self.draw()
144 def press1_key(self, event):# drawer
145 if keys.has_key(event.char):
146 column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
147 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
148 automata.field[row][column] = automata.states[keys[event.char]].symbol
149 self.draw()
151 def motion1_key(self, event):# drawer
152 if keys.has_key(event.char):
153 column = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
154 row = (event.y - self.offset_y * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width)
155 automata.field[row][column] = automata.states[keys[event.char]].symbol
156 self.draw()
158 def press1_ctrl(self, event):# change_scale (B1+ctrl)
159 self.mouse_x = event.x
160 self.mouse_y = event.y
162 def motion1_ctrl(self, event):# change_scale (B1+ctrl)
163 self.offset_x = (event.x - self.mouse_x) / (self.cell_size + self.line_width)
164 self.offset_y = (event.y - self.mouse_y) / (self.cell_size + self.line_width)
165 self.mouse_x = event.x
166 self.mouse_y = event.y
167 self.draw()
169 def press12(self, event):# zoom
170 self.mouse_zoom = event.y
172 def motion12(self, event):# zoom
173 delta = (event.y - self.mouse_zoom) / self.zoom_divisor
174 self.cell_size = self.cell_size + delta
175 if self.cell_size > 50:
176 self.cell_size = 50
177 if self.cell_size < 1:
178 self.cell_size = 1
179 self.mouse_zoom = event.y
180 self.draw()
182 def automata_frame(self):# show automata_frame
183 automata_frame.pack(side="right", fill="y", expand="no", before=canvas)
185 def to_top(self):# replace choosen state to top
186 index = automata.symbols.get(state_list.get("active").split()[1])
187 state = automata.states[index]
188 del automata.states[index]
189 automata.states.insert(0, state)
191 def to_bottom(self):# replace choosen state to botton
192 index = automata.symbols.get(state_list.get("active").split()[1])
193 state = automata.states[index]
194 del automata.states[index]
195 automata.states.append(state)
197 def upwards(self):
198 index = automata.symbols.get(state_list.get("active").split()[1])
199 state = automata.states[index]
200 del automata.states[index]
201 automata.states.insert(index - 1, state)
203 def downwards(self):
204 index = automata.symbols.get(state_list.get("active").split()[1])
205 state = automata.states[index]
206 del automata.states[index]
207 automata.states.insert(index + 1, state)
209 def delete_state(self):# delete choosen state
210 index = automata.symbols.get(state_list.get("active").split()[1])
211 del automata.states[index]
213 def add(self):# add new state
214 pass
215 name = state_name.get()
216 symbol = state_sympob.get()
217 key = lower(state_key.get())
218 color = state_color.get()
219 nums = []
220 for i, value in enumerate(ckeckbox_nums):
221 if value:
222 nums.append(i)
223 if self.keys.has_key[key]:
224 error.config(text="State with such key has already existed")
225 state_key.focus()
226 elif len(key) != 1:
227 error.config(text="Bad key for state")
228 state_key.focus()
229 elif automata.symbols.has_key(symbol):
230 error.config(text="State with such symbol has already existed")
231 state_symbol.focus()
232 elif len(symbol) != 1:
233 error.config(text="Bad symbol for state")
234 state_symbol.focus()
235 else:
236 state = State(name, symbol, color, nums)
237 automata.states.append(state)
238 automata.symbols[symbol] = len(automata.states) - 1
239 self.keys[key] = len(automata.states) - 1
241 def change(self):# change chosen state
242 pass
243 def show_size_window(self):
244 size_window.deiconify()
245 def hide_size_window(self):
246 size_window.withdraw()
249 root = Tk()
250 root.title("Cyclic Cellular Automata")
252 canvas = Canvas(root, background="white")
253 canvas.config(width=500, height=400)
255 automata = Automata(50, 50)
256 handlers = Handlers()
258 canvas.bind("<1>", handlers.press1)
259 canvas.bind("<B1-Motion>", handlers.motion1)
260 canvas.bind("<3>", handlers.press3)
261 canvas.bind("<B3-Motion>", handlers.motion3)
262 canvas.bind("<Key><ButtonPress-1>", handlers.press1_key)
263 canvas.bind("<Key><B1-Motion>", handlers.motion1_key)
264 canvas.bind("<Control-ButtonPress-1>", handlers.press1_ctrl)
265 canvas.bind("<Control-B1-Motion>", handlers.motion1_ctrl)
267 canvas.pack(fill="both", expand="yes")
269 states = []
270 symboles = dict()
272 #infoPanel=Frame
273 automata_frame=Frame(root, background="grey")
275 headline_frame=Frame(automata_frame, background="white")
276 head = Label(headline_frame, text= "Automata Panel", font=16)
277 head.pack(side="left", expand="yes")
278 hide = Button(headline_frame, text="X", command=automata_frame.forget)
279 hide.config(bg="grey")
280 hide.pack(side="right")
281 headline_frame.pack(side="top",fill="both", expand="no")
283 Label(automata_frame, text= "State Box:").pack(side="top", fill="x")
284 state_list=Listbox(automata_frame, selectmode="extended")
285 for state in automata.states:
286 state_list.insert("end", state)
287 state_list.pack(side="top", fill="y")
288 up = Button(automata_frame, text="Up", state="disabled")
289 up.config(bg="red")
290 down = Button(automata_frame, text="Down", state="disabled")
291 down.config(bg="orange")
292 to_top = Button(automata_frame, text="To Top", state="disabled")
293 to_top.config(bg="yellow")
294 to_bottom = Button(automata_frame, text="To Bottom", state="disabled")
295 to_bottom.config(bg="green")
296 delete = Button(automata_frame, text="Delete", state="disabled")
297 delete.config(bg="cyan")
298 up.pack(side="top", fill="x")
299 down.pack(side="top", fill="x")
300 to_top.pack(side="top", fill="x")
301 to_bottom.pack(side="top", fill="x")
302 delete.pack(side="top", fill="x")
305 information = Label(automata_frame, text= "Information of State")
306 information.pack(side="top", fill="x")
307 info_frame=Frame(automata_frame, background="white")
308 Label(info_frame, text="Name").grid(row=0, column=0)
309 state_name = Entry(info_frame)
310 state_name.grid(row=0, column=1)
311 Label(info_frame, text="Symbol").grid(row=1, column=0)
312 state_symbol = Entry(info_frame)
313 state_symbol.grid(row=1, column=1)
314 Label(info_frame, text="Color").grid(row=2, column=0)
315 state_color = Entry(info_frame)
316 state_color.grid(row=2, column=1)
317 Label(info_frame, text="Key").grid(row=3, column=0)
318 state_key = Entry(info_frame)
319 state_key.grid(row=3, column=1)
320 info_frame.pack(side="top")
323 ckeckbox_nums = [IntVar(), IntVar(), IntVar(), IntVar(), IntVar(),
324 IntVar(), IntVar(), IntVar(), IntVar()]
326 condition = Label(automata_frame, text= "Condition of conversion")
327 condition.pack(side="top", fill="x")
328 condition_frame=Frame(automata_frame, background="white")
329 Label(condition_frame, text="0: ").grid(row=0, column=0)
330 c_button_0 = Checkbutton(condition_frame, variable=check_box[0])
331 c_button_0.grid(row=0, column=1)
332 Label(condition_frame, text="1: ").grid(row=0, column=2)
333 c_button_1 = Checkbutton(condition_frame, variable=check_box[1])
334 c_button_1.grid(row=0, column=3)
335 Label(condition_frame, text="2: ").grid(row=0, column=4)
336 c_button_2 = Checkbutton(condition_frame, variable=check_box[2])
337 c_button_2.grid(row=0, column=5)
338 Label(condition_frame, text="3: ").grid(row=1, column=0)
339 c_button_3 = Checkbutton(condition_frame, variable=check_box[3])
340 c_button_3.grid(row=1, column=1)
341 Label(condition_frame, text="4: ").grid(row=1, column=2)
342 c_button_4 = Checkbutton(condition_frame, variable=check_box[4])
343 c_button_4.grid(row=1, column=3)
344 Label(condition_frame, text="5: ").grid(row=1, column=4)
345 c_button_5 = Checkbutton(condition_frame, variable=check_box[5])
346 c_button_5.grid(row=1, column=5)
347 Label(condition_frame, text="6: ").grid(row=2, column=0)
348 c_button_6 = Checkbutton(condition_frame, variable=check_box[6])
349 c_button_6.grid(row=2, column=1)
350 Label(condition_frame, text="7: ").grid(row=2, column=2)
351 c_button_7 = Checkbutton(condition_frame, variable=check_box[7])
352 c_button_7.grid(row=2, column=3)
353 Label(condition_frame, text="8: ").grid(row=2, column=4)
354 c_button_8 = Checkbutton(condition_frame, variable=check_box[8])
355 c_button_8.grid(row=2, column=5)
356 condition_frame.pack(side="top")
359 add_state = Button(automata_frame, text="ADD", command=handlers.add)
360 add_state.config(bg="blue")
361 change_state = Button(automata_frame, text="Change", state="disabled")
362 change_state.config(bg="violet")
363 add_state.pack(side="top", fill="x")
364 change_state.pack(side="top", fill="x")
366 error=Label(automata_frame)
367 error.pack(side="top", fill="x")
370 side = 0
372 size_window = Toplevel(root)
373 size_window.title("")
374 size_window.withdraw()
375 size_window.protocol("WM_DELETE_WINDOW", handlers.hide_size_window)
376 Label(size_window, text= "Current size of window:").pack(side="top", fill="x")
377 size = Label(size_window, text= str(len(automata.field)) + " x " + str(len(automata.field[0])))
378 size.pack(side="top", fill="x")
379 Label(size_window, text= "New size:").pack(side="top", fill="x")
380 new_size = Frame(size_window)
381 size_x = Entry(new_size, width=5)
382 size_x.grid(row=0, column=0)
383 Label(new_size, text=" x ").grid(row=0, column=1)
384 size_y = Entry(new_size, width=5)
385 size_y.grid(row=0, column=2)
386 new_size.pack(side="top")
387 Label(size_window, text= "Expansion of window:").pack(side="top", fill="x")
388 expansion = Frame(size_window)
389 r0 = Radiobutton(expansion, variable=side, value = 0, indicatoron=0, width=2, height=1)
390 r0.select()
391 r0.grid(row=0, column=0)
392 r1 = Radiobutton(expansion, variable=side, value = 1, indicatoron=0, width=2, height=1)
393 r1.grid(row=0, column=1)
394 r2 = Radiobutton(expansion, variable=side, value = 2, indicatoron=0, width=2, height=1)
395 r2.grid(row=0, column=2)
396 r3 = Radiobutton(expansion, variable=side, value = 3, indicatoron=0, width=2, height=1)
397 r3.grid(row=1, column=0)
398 r4 = Radiobutton(expansion, variable=side, value = 4, indicatoron=0, width=2, height=1)
399 r4.grid(row=1, column=1)
400 r5 = Radiobutton(expansion, variable=side, value = 5, indicatoron=0, width=2, height=1)
401 r5.grid(row=1, column=2)
402 r6 = Radiobutton(expansion, variable=side, value = 6, indicatoron=0, width=2, height=1)
403 r6.grid(row=2, column=0)
404 r7 = Radiobutton(expansion, variable=side, value = 7, indicatoron=0, width=2, height=1)
405 r7.grid(row=2, column=1)
406 r8 = Radiobutton(expansion, variable=side, value = 8, indicatoron=0, width=2, height=1)
407 r8.grid(row=2, column=2)
408 expansion.pack(side="top")
409 Label(size_window).pack(side="top", fill="x")
410 apply_frame = Frame(size_window, padx=10, pady=5)
411 apply_size = Button(apply_frame, text="Apply")
412 apply_size.config(bg="yellow")
413 apply_size.pack(side="left", fill="x")
414 close_size = Button(apply_frame, text="Close", command=handlers.hide_size_window)
415 close_size.config(bg="green")
416 close_size.pack(side="right", fill="x")
417 apply_frame.pack(side="top", fill="x")
419 menubar = Menu(root)
420 root.config(menu=menubar)
422 menu_file = Menu(menubar)
423 menu_file.add_command(label="New")
424 menu_file.add_command(label="Open...", command=handlers.open_file)
425 menu_file.add_command(label="Save...", command=handlers.save_file)
426 menu_file.add_separator()
427 menu_file.add_command(label="Exit", command=root.destroy)
428 menubar.add_cascade(label="File", menu=menu_file)
430 menu_action = Menu(menubar)
431 menu_action.add_command(label="Start", command=handlers.start)
432 menu_action.add_command(label="Stop", command=handlers.stop)
433 menu_action.add_command(label="Next Step", command=handlers.next_step)
434 menu_action.add_separator()
435 menu_action.add_command(label="Increase speed", command=handlers.faster)
436 menu_action.add_command(label="Decrease speed", command=handlers.slower)
437 menu_action.add_separator()
438 menu_action.add_command(label="Zoom In", command=handlers.zoom_in)
439 menu_action.add_command(label="Zoom Out", command=handlers.zoom_out)
440 menu_action.add_separator()
441 menu_action.add_command(label="Clean field")
442 menu_action.add_command(label="Fill randomly")
443 menu_action.add_separator()
444 menu_action.add_command(label="Change size",command=handlers.show_size_window)
445 menubar.add_cascade(label="Action", menu=menu_action)
447 menubar.add_command(label="Automata", command=handlers.automata_frame)
449 menubar.add_command(label="Help", command=handlers.help)
451 root.mainloop()