Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/annotate/d2705c3ee7a7/Interface.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 23:50:54 2014
Кодировка:
cca: Interface.py annotate

cca

annotate Interface.py @ 18:d2705c3ee7a7

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