Changeset 69:ac96d7c2c7c4
- Timestamp:
- 12/11/10 22:32:04 (5 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Interface.py
r66 r69 1 1 import math 2 import random 2 3 import tkColorChooser 3 4 from Tkinter import * ? ? 145 146 try: 146 147 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 if self.char != None and self.char in self.keys: 148 149 index = self.keys[self.char] 149 150 automata.field[row][col] = automata.states[index].symbol ? ? 158 159 if self.col1 >= 0 and self.row1 >= 0: 159 160 self.new_state([(self.row1, self.col1)]) 160 161 161 162 def motion1(self, event):# drawer 162 163 col = (event.x - self.offset_x * (self.cell_size + self.line_width)) / (self.cell_size + self.line_width) ? ? 224 225 index = selected 225 226 state = automata.states[index] 226 print state227 227 del automata.states[index] 228 228 automata.states.insert(0, state) 229 229 self.selected_state = 0 230 self.refresh_dicts() 230 231 self.refresh_list() 231 232 ? ? 235 236 index = selected 236 237 state = automata.states[index] 237 print state238 238 del automata.states[index] 239 239 automata.states.append(state) 240 240 self.selected_state = len(automata.states) - 1 241 self.refresh_dicts() 241 242 self.refresh_list() 242 243 ? ? 247 248 if index > 0: 248 249 state = automata.states[index] 249 print state250 250 del automata.states[index] 251 251 automata.states.insert(index - 1, state) 252 252 self.selected_state = index - 1 253 self.refresh_dicts() 253 254 self.refresh_list() 254 255 ? ? 259 260 if index < state_list.size() - 1: 260 261 state = automata.states[index] 261 print state262 262 del automata.states[index] 263 263 automata.states.insert(index + 1, state) 264 264 self.selected_state = index + 1 265 self.refresh_dicts() 265 266 self.refresh_list() 266 267 267 268 def delete_state(self):# delete choosen state 268 269 selected = self.selected_state 269 if selected != None : 270 if selected != None and len(automata.states) != 1: 270 271 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.st_sym[automata.states[index].symbol] 272 symbol = automata.states[index].symbol 277 273 del automata.states[index] 274 self.refresh_dicts() 278 275 if index in automata.states: 279 276 self.selected_state = index 280 el if len(automata.states): 277 else: 281 278 self.selected_state = len(automata.states) - 1 282 else:283 self.selected_state = None284 279 self.refresh_list() 285 280 self.select_item(self.selected_state) 281 self.draw_changed_state(symbol, automata.states[0].symbol) 282 else: 283 error.config(text="Operation is refused") 284 error.after(2000, self.clear_error) 286 285 287 286 def add(self):# add new state ? ? 296 295 if self.keys.has_key(key): 297 296 error.config(text="State with such key has already existed") 297 error.after(2000, self.clear_error) 298 298 state_key.focus() 299 299 elif len(key) != 1: 300 300 error.config(text="Bad key for state") 301 error.after(2000, self.clear_error) 301 302 state_key.focus() 302 303 elif automata.symbols.has_key(symbol): 303 304 error.config(text="State with such symbol has already existed") 305 error.after(2000, self.clear_error) 304 306 state_symbol.focus() 305 307 elif len(symbol) != 1: 306 308 error.config(text="Bad symbol for state") 309 error.after(2000, self.clear_error) 307 310 state_symbol.focus() 308 311 else: 309 state = State(name, symbol, color, nums) 312 state = State(name, symbol, color, key, nums) 310 313 automata.states.append(state) 311 314 automata.symbols[symbol] = len(automata.states) - 1 ? ? 331 334 if self.keys.has_key(key) and self.keys[key] != index: 332 335 error.config(text="State with such key has already existed") 336 error.after(2000, self.clear_error) 333 337 state_key.focus() 334 338 elif len(key) != 1: 335 339 error.config(text="Bad key for state") 340 error.after(2000, self.clear_error) 336 341 state_key.focus() 337 342 elif automata.symbols.has_key(symbol) and automata.symbols[symbol] != index: 338 343 error.config(text="State with such symbol has already existed") 344 error.after(2000, self.clear_error) 339 345 state_symbol.focus() 340 346 elif len(symbol) != 1: 341 347 error.config(text="Bad symbol for state") 348 error.after(2000, self.clear_error) 342 349 state_symbol.focus() 343 350 else: 344 state = State(name, symbol, color, nums) 345 del automata.symbols[automata.states[index].symbol] 346 automata.symbols[symbol] = index 347 del automata.st_sym[automata.states[index].symbol] 348 automata.st_sym[symbol] = state 349 self.keys[key] = index 350 error.config(text="") 351 state = State(name, symbol, color, key, nums) 352 self.draw_changed_state(automata.states[index].symbol, symbol) 351 353 automata.states[index] = state 354 self.refresh_dicts() 352 355 self.refresh_list() 356 357 def draw_changed_state(self, symbol_old, symbol_new): 358 cells = [] 359 for row in range(automata.height): 360 for col in range(automata.width): 361 if automata.field[row][col] == symbol_old: 362 automata.field[row][col] = symbol_new 363 cells.append((row, col)) 364 365 self.draw_cell(cells) 353 366 354 367 def show_size_window(self): ? ? 398 411 if len(selected): 399 412 self.select_item(self.selected_state) 413 400 414 def choose_color(self, event): 401 415 state_color.config(bg=tkColorChooser.askcolor()[1]) 416 417 def clear_error(self): 418 error.config(text="") 419 420 def clean_field(self): 421 symbol = automata.states[0].symbol 422 cells = [] 423 for row in range(automata.height): 424 for col in range(automata.width): 425 if automata.field[row][col] != symbol: 426 automata.field[row][col] = symbol 427 cells.append((row, col)) 428 self.draw_cell(cells) 429 430 def fill_randomly(self): 431 cells = [] 432 for row in range(automata.height): 433 for col in range(automata.width): 434 automata.field[row][col] = automata.states[random.randint(0, len(automata.states) - 1)].symbol 435 cells.append((row, col)) 436 self.draw_cell(cells) 437 438 def refresh_dicts(self): 439 automata.symbols = {} 440 automata.st_sym = {} 441 self.keys = {} 442 for index, state in enumerate(automata.states): 443 automata.symbols[state.symbol] = index 444 automata.st_sym[state.symbol] = state 445 self.keys[state.key] = index 446 402 447 403 448 ? ? 600 645 menu_action.add_command(label="Zoom Out", command=handlers.zoom_out) 601 646 menu_action.add_separator() 602 menu_action.add_command(label="Clean field" )603 menu_action.add_command(label="Fill randomly" ) 647 menu_action.add_command(label="Clean field", command=handlers.clean_field) 648 menu_action.add_command(label="Fill randomly", command=handlers.fill_randomly) 604 649 menu_action.add_separator() 605 650 menu_action.add_command(label="Change size",command=handlers.show_size_window)
Note: See TracChangeset
for help on using the changeset viewer.