Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/annotate/21a5779088e6/main.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 19:05:07 2014
Кодировка:
snake: main.py annotate

snake

annotate main.py @ 42:21a5779088e6

main.py: added if __name__ == "__main__" condition to automatic ui startup
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Sun, 19 Dec 2010 22:54:58 +0300
parents b2eaeeb74d87
children a3bb04e72924
rev   line source
martiran@18 1 import Tkinter as tk
Alex@23 2 import tkFileDialog as tkfd
martiran@20 3 import engine
martiran@32 4 import snake
martiran@18 5
martiran@18 6
martiran@18 7
martiran@18 8 class UI(object):
martiran@19 9 def __init__ (self):
martiran@29 10 self.root = tk.Tk()
martiran@29 11 self.root.title("Python Battle")
martiran@29 12 self.canvas = tk.Canvas(self.root, background = "black")
martiran@19 13 self.canvas.pack(side ="top", fill="both", expand="yes")
martiran@29 14 buttons = tk.Frame(self.root)
martiran@20 15 buttons.pack(side ="bottom", fill="both", expand="yes")
martiran@20 16 self.buttons_pack(buttons)
martiran@28 17 self.step_id = 0
martiran@19 18 self.engine = engine.Engine(self.canvas)
martiran@32 19 self.after_id = None
martiran@18 20 return
martiran@20 21 def buttons_pack(self, frame):
martiran@32 22 load_1 = tk.Button(frame, text="Load 1", command=lambda: self.load(1))
martiran@32 23 load_1.pack(side="left", fill="both", expand = "yes")
martiran@32 24 load_2 = tk.Button(frame, text="Load 2", command=lambda: self.load(2))
martiran@32 25 load_2.pack(side="left", fill="both", expand = "yes")
martiran@28 26 run_b = tk.Button(frame, text="Run", command=lambda: self.run())
martiran@32 27 run_b.pack(side="left", fill="both", expand = "yes")
martiran@32 28 load_3 = tk.Button(frame, text="Load 3", command=lambda: self.load(3))
martiran@32 29 load_3.pack(side="right", fill="both", expand = "yes")
martiran@32 30 load_4 = tk.Button(frame, text="Load 4", command=lambda: self.load(4))
martiran@32 31 load_4.pack(side="right", fill="both", expand = "yes")
martiran@28 32 step_b = tk.Button(frame, text="Step", command=lambda: self.step())
martiran@32 33 step_b.pack(side="right", fill="both", expand = "yes")
martiran@20 34 return
martiran@29 35
Alex@23 36 def load (self, snake_number):
martiran@29 37 if self.step_id >= 200:
martiran@29 38 self.step_id = 0
martiran@29 39 pass
martiran@29 40 elif self.step_id == 0:
martiran@29 41 file_name = tkfd.askopenfilename(title="Open file")
martiran@29 42 snake = self.engine.create_snake(snake_number)
martiran@29 43 snake.load(file_name)
martiran@29 44 pass
martiran@29 45 else:
martiran@29 46 pass
Alex@27 47 return
Alex@23 48
martiran@18 49 def run (self):
martiran@32 50 self.step_id = self.step_id+1
martiran@28 51 self.engine.step()
martiran@28 52 self.after_id = self.canvas.after(300, self.run())
martiran@28 53 if self.step_id == 200:
martiran@28 54 self.canvas.after_cancel(self.after_id)
martiran@28 55 self.end()
martiran@28 56 pass
martiran@28 57 return
martiran@28 58 def step (self):
martiran@32 59 if self.step_id <= 200:
martiran@32 60 if self.after_id != None:
martiran@32 61 self.canvas.after_cancel(self.after_id)
martiran@32 62 pass
martiran@32 63 self.step_id = self.step_id+1
martiran@29 64 self.engine.step()
martiran@29 65 pass
martiran@29 66 else:
martiran@29 67 self.end()
martiran@29 68 pass
martiran@29 69 return
Alex@23 70
martiran@28 71 def end (self):
martiran@29 72 root = tk.Tk()
martiran@29 73 end_label = tk.Label(root, text="End")
martiran@29 74 end_label.pack()
martiran@29 75 root.mainloop()
martiran@18 76 pass
martiran@32 77
me@42 78 if __name__ == "__main__":
me@42 79 snake_batle = UI()
me@42 80 snake_batle.root.mainloop()