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

cca

view Interface.py @ 23:c74a9885bf04

Comment in English
author darkhan
date Sat, 04 Dec 2010 23:44:37 +0300
parents d2705c3ee7a7
children c27ba2bc1f80
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 def start(self):
23 if not self.is_started:
24 self.is_started = True
25 self.next_step()
26 self.after_id = canvas.after(self.delay, self.start)
28 def stop(self):
29 self.canvas.after_cancel(self.after_id)
30 self.is_started = False
32 def next_step(self):
33 automata.next_step()
34 self.draw()
36 def save_file(self):
37 pass
39 def open_file(self):
40 pass
42 def help(self):
43 pass
45 def close_help_window(self):
46 pass
48 def zoom_in(self, zoom_rate=1):
49 if self.cell_size < 50:
50 self.cell_size = self.cell_size + zoom_rate
51 self.draw()
53 def zoom_out(self, zoom_rate=1):
54 if self.cell_size > 1:
55 self.cell_size = self.cell_size - zoom_rate
56 self.draw()
58 def slower(self, speed_rate=1):
59 self.delay = self.delay + speed_rate
61 def faster(self, speed_rate=1):
62 if self.speed > speed_rate:
63 self.delay = self.delay - speed_rate
64 else:
65 self.delay = 0
67 def change_size(self, dx, dy, position=0):
68 if position < 9:
69 if position == 0 or position == 3 or position == 6:
70 automata.change_size(dx, 3)
71 elif position == 1 or position == 4 or position == 7:
72 automata.change_size(dx / 2, 3)
73 automata.change_size(dx - dx / 2, 1)
74 else:
75 automata.change_size(dx, 1)
76 if position == 0 or position == 1 or position == 2:
77 automata.change_size(dy, 0)
78 elif position == 3 or position == 4 or position == 5:
79 automata.change_size(dy / 2, 0)
80 automata.change_size(dy - dy / 2, 2)
81 else:
82 automata.change_size(dy, 2)
83 self.draw()
85 def draw():
86 pass
88 def press1(self, event):# drawer
89 column = (event.x - self.offset_x) / (self.cell_size + self.line_width)
90 row = (event.y - self.offset_y) / (self.cell_size + self.line_width)
91 index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states)
92 automata.field[row][column] = automata.states[index].symbol
93 self.draw()
95 def motion1(self, event):# drawer
96 column = (event.x - self.offset_x) / (self.cell_size + self.line_width)
97 row = (event.y - self.offset_y) / (self.cell_size + self.line_width)
98 index = (automata.symbols[automata.field[row][column]] + 1) % len(automata.states)
99 automata.field[row][column] = automata.states[index].symbol
100 self.draw()
102 def press3(self, event):# drawer
103 column = (event.x - self.offset_x) / (self.cell_size + self.line_width)
104 row = (event.y - self.offset_y) / (self.cell_size + self.line_width)
105 index = (automata.symbols[automata.field[row][column]] + len(automata.states) - 1) % len(automata.states)
106 automata.field[row][column] = automata.states[index].symbol
107 self.draw()
109 def motion3(self, event):# drawer
110 column = (event.x - self.offset_x) / (self.cell_size + self.line_width)
111 row = (event.y - self.offset_y) / (self.cell_size + self.line_width)
112 index = (automata.symbols[automata.field[row][column]] + len(automata.states) - 1) % len(automata.states)
113 automata.field[row][column] = automata.states[index].symbol
114 self.draw()
116 def press1_key(self, event):# drawer
117 if keys.has_key(event.char):
118 column = (event.x - self.offset_x) / (self.cell_size + self.line_width)
119 row = (event.y - self.offset_y) / (self.cell_size + self.line_width)
120 automata.field[row][column] = automata.states[keys[event.char]].symbol
121 self.draw()
123 def motion1_key(self, event):# drawer
124 if keys.has_key(event.char):
125 column = (event.x - self.offset_x) / (self.cell_size + self.line_width)
126 row = (event.y - self.offset_y) / (self.cell_size + self.line_width)
127 automata.field[row][column] = automata.states[keys[event.char]].symbol
128 self.draw()
130 def press1_ctrl(self, event):# change_scale (B1+ctrl)
131 self.mouse_x = event.x
132 self.mouse_y = event.y
134 def motion1_ctrl(self, event):# change_scale (B1+ctrl)
135 self.offset_x = event.x - self.mouse_x
136 self.offset_y = event.y - self.mouse_y
137 self.mouse_x = event.x
138 self.mouse_y = event.y
139 self.draw()
141 def press12(self, event):# zoom
142 self.mouse_zoom = event.y
144 def motion12(self, event):# zoom
145 delta = (event.y - self.mouse_zoom) / self.zoom_divisor
146 self.cell_size = self.cell_size + delta
147 if self.cell_size > 50:
148 self.cell_size = 50
149 if self.cell_size < 1:
150 self.cell_size = 1
151 self.mouse_zoom = event.y
152 self.draw()
154 def automata_frame(self):# show automata_frame
155 automata_frame.pack(side="right", fill="y", expand="no", before=canvas)
157 def to_top(self):# replace choosen state to top
158 index = symbols.get(state_list.get("active").split()[1])
159 state = states[index]
160 del states[index]
161 states.insert(0, state)
163 def to_bottom(self):# replace choosen state to botton
164 index = symbols.get(state_list.get("active").split()[1])
165 state = states[index]
166 del states[index]
167 states.append(state)
169 def upwards(self):
170 index = symbols.get(state_list.get("active").split()[1])
171 state = states[index]
172 del states[index]
173 states.insert(index - 1, state)
175 def downwards(self):
176 index = symbols.get(state_list.get("active").split()[1])
177 state = states[index]
178 del states[index]
179 states.insert(index + 1, state)
181 def delete_state(self):# delete choosen state
182 index = symbols.get(state_list.get("active").split()[1])
183 del states[index]
185 def add(self):# add new state
186 pass
188 def change(self):# change chosen state
189 pass
190 def show_size_window(self):
191 size_window.deiconify()
192 def hide_size_window(self):
193 size_window.withdraw()
196 root = Tk()
197 root.title("Cyclyc Cell Automata")
199 canvas = Canvas(root, background="white")
200 canvas.config(width=500, height=400)
201 canvas.pack(fill="both", expand="yes")
203 automata = Automata()
204 handlers = Handlers(1, 1, 0, 0)
206 states = []
207 symboles = dict()
209 #infoPanel=Frame
210 automata_frame=Frame(root, background="grey")
212 headline_frame=Frame(automata_frame, background="white")
213 head = Label(headline_frame, text= "Automata Panel", font=16)
214 head.pack(side="left", expand="yes")
215 hide = Button(headline_frame, text="X", command=automata_frame.forget)
216 hide.config(bg="grey")
217 hide.pack(side="right")
218 headline_frame.pack(side="top",fill="both", expand="no")
220 Label(automata_frame, text= "State Box:").pack(side="top", fill="x")
221 state_list=Listbox(automata_frame, selectmode="extended")
222 for state in states:
223 state_list.insert("end", state)
224 state_list.pack(side="top", fill="y")
225 up = Button(automata_frame, text="Up", state="disabled")
226 up.config(bg="red")
227 down = Button(automata_frame, text="Down", state="disabled")
228 down.config(bg="orange")
229 to_top = Button(automata_frame, text="To Top", state="disabled")
230 to_top.config(bg="yellow")
231 to_bottom = Button(automata_frame, text="To Bottom", state="disabled")
232 to_bottom.config(bg="green")
233 delete = Button(automata_frame, text="Delete", state="disabled")
234 delete.config(bg="cyan")
235 up.pack(side="top", fill="x")
236 down.pack(side="top", fill="x")
237 to_top.pack(side="top", fill="x")
238 to_bottom.pack(side="top", fill="x")
239 delete.pack(side="top", fill="x")
242 information = Label(automata_frame, text= "Information of State")
243 information.pack(side="top", fill="x")
244 info_frame=Frame(automata_frame, background="white")
245 Label(info_frame, text="Name").grid(row=0, column=0)
246 state_name = Entry(info_frame)
247 state_name.grid(row=0, column=1)
248 Label(info_frame, text="Symbol").grid(row=1, column=0)
249 state_symbol = Entry(info_frame)
250 state_symbol.grid(row=1, column=1)
251 Label(info_frame, text="Color").grid(row=2, column=0)
252 state_color = Entry(info_frame)
253 state_color.grid(row=2, column=1)
254 Label(info_frame, text="Key").grid(row=3, column=0)
255 state_key = Entry(info_frame)
256 state_key.grid(row=3, column=1)
257 info_frame.pack(side="top")
260 condition = Label(automata_frame, text= "Condition of conversion")
261 condition.pack(side="top", fill="x")
262 condition_frame=Frame(automata_frame, background="white")
263 Label(condition_frame, text="0: ").grid(row=0, column=0)
264 check_box_0 = Checkbutton(condition_frame)
265 check_box_0.grid(row=0, column=1)
266 Label(condition_frame, text="1: ").grid(row=0, column=2)
267 check_box_1 = Checkbutton(condition_frame)
268 check_box_1.grid(row=0, column=3)
269 Label(condition_frame, text="2: ").grid(row=0, column=4)
270 check_box_2 = Checkbutton(condition_frame)
271 check_box_2.grid(row=0, column=5)
272 Label(condition_frame, text="3: ").grid(row=1, column=0)
273 check_box_0 = Checkbutton(condition_frame)
274 check_box_0.grid(row=1, column=1)
275 Label(condition_frame, text="4: ").grid(row=1, column=2)
276 check_box_1 = Checkbutton(condition_frame)
277 check_box_1.grid(row=1, column=3)
278 Label(condition_frame, text="5: ").grid(row=1, column=4)
279 check_box_2 = Checkbutton(condition_frame)
280 check_box_2.grid(row=1, column=5)
281 Label(condition_frame, text="6: ").grid(row=2, column=0)
282 check_box_0 = Checkbutton(condition_frame)
283 check_box_0.grid(row=2, column=1)
284 Label(condition_frame, text="7: ").grid(row=2, column=2)
285 check_box_1 = Checkbutton(condition_frame)
286 check_box_1.grid(row=2, column=3)
287 Label(condition_frame, text="8: ").grid(row=2, column=4)
288 check_box_2 = Checkbutton(condition_frame)
289 check_box_2.grid(row=2, column=5)
290 condition_frame.pack(side="top")
293 add_state = Button(automata_frame, text="ADD", state="disabled")
294 add_state.config(bg="blue")
295 change_state = Button(automata_frame, text="Change", state="disabled")
296 change_state.config(bg="violet")
297 add_state.pack(side="top", fill="x")
298 change_state.pack(side="top", fill="x")
300 error=Label(automata_frame)
301 error.pack(side="top", fill="x")
304 side = 0
306 size_window = Toplevel(root)
307 size_window.title("")
308 size_window.withdraw()
309 size_window.protocol("WM_DELETE_WINDOW", handlers.hide_size_window)
310 Label(size_window, text= "Current size of window:").pack(side="top", fill="x")
311 size = Label(size_window, text= str(len(automata.field)) + " x " + str(len(automata.field[0])))
312 size.pack(side="top", fill="x")
313 Label(size_window, text= "New size:").pack(side="top", fill="x")
314 new_size = Frame(size_window)
315 size_x = Entry(new_size, width=5)
316 size_x.grid(row=0, column=0)
317 Label(new_size, text=" x ").grid(row=0, column=1)
318 size_y = Entry(new_size, width=5)
319 size_y.grid(row=0, column=2)
320 new_size.pack(side="top")
321 Label(size_window, text= "Expansion of window:").pack(side="top", fill="x")
322 expansion = Frame(size_window)
323 r0 = Radiobutton(expansion, variable=side, value = 0, indicatoron=0, width=2, height=1)
324 r0.select()
325 r0.grid(row=0, column=0)
326 r1 = Radiobutton(expansion, variable=side, value = 1, indicatoron=0, width=2, height=1)
327 r1.grid(row=0, column=1)
328 r2 = Radiobutton(expansion, variable=side, value = 2, indicatoron=0, width=2, height=1)
329 r2.grid(row=0, column=2)
330 r3 = Radiobutton(expansion, variable=side, value = 3, indicatoron=0, width=2, height=1)
331 r3.grid(row=1, column=0)
332 r4 = Radiobutton(expansion, variable=side, value = 4, indicatoron=0, width=2, height=1)
333 r4.grid(row=1, column=1)
334 r5 = Radiobutton(expansion, variable=side, value = 5, indicatoron=0, width=2, height=1)
335 r5.grid(row=1, column=2)
336 r6 = Radiobutton(expansion, variable=side, value = 6, indicatoron=0, width=2, height=1)
337 r6.grid(row=2, column=0)
338 r7 = Radiobutton(expansion, variable=side, value = 7, indicatoron=0, width=2, height=1)
339 r7.grid(row=2, column=1)
340 r8 = Radiobutton(expansion, variable=side, value = 8, indicatoron=0, width=2, height=1)
341 r8.grid(row=2, column=2)
342 expansion.pack(side="top")
343 Label(size_window).pack(side="top", fill="x")
344 apply_frame = Frame(size_window, padx=10, pady=5)
345 apply_size = Button(apply_frame, text="Apply")
346 apply_size.config(bg="yellow")
347 apply_size.pack(side="left", fill="x")
348 close_size = Button(apply_frame, text="Close", command=handlers.hide_size_window)
349 close_size.config(bg="green")
350 close_size.pack(side="right", fill="x")
351 apply_frame.pack(side="top", fill="x")
353 menubar = Menu(root)
354 root.config(menu=menubar)
356 menu_file = Menu(menubar)
357 menu_file.add_command(label="New")
358 menu_file.add_command(label="Open...", command=handlers.open_file)
359 menu_file.add_command(label="Save...", command=handlers.save_file)
360 menu_file.add_separator()
361 menu_file.add_command(label="Exit", command=root.destroy)
362 menubar.add_cascade(label="File", menu=menu_file)
364 menu_action = Menu(menubar)
365 menu_action.add_command(label="Start", command=handlers.start)
366 menu_action.add_command(label="Stop", command=handlers.stop)
367 menu_action.add_command(label="Next Step", command=handlers.next_step)
368 menu_action.add_separator()
369 menu_action.add_command(label="Increase speed", command=handlers.faster)
370 menu_action.add_command(label="Decrease speed", command=handlers.slower)
371 menu_action.add_separator()
372 menu_action.add_command(label="Zoom In", command=handlers.zoom_in)
373 menu_action.add_command(label="Zoom Out", command=handlers.zoom_out)
374 menu_action.add_separator()
375 menu_action.add_command(label="Clean field")
376 menu_action.add_command(label="Fill randomly")
377 menu_action.add_separator()
378 menu_action.add_command(label="Change size",command=handlers.show_size_window)
379 menubar.add_cascade(label="Action", menu=menu_action)
381 menubar.add_command(label="Automata", command=handlers.automata_frame)
383 menubar.add_command(label="Help", command=handlers.help)
385 root.mainloop()