Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/file/af9337dd3cf1/main.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 06:55:37 2013
Кодировка:
snake: af9337dd3cf1 main.py

snake

view main.py @ 29:af9337dd3cf1

corrected UI.load(), corrected UI.step(), UI.end(), root made self.root
author Alex Martynov <martiran@kodomo.fbb.msu.ru>
date Sun, 19 Dec 2010 15:43:53 +0300
parents 06f6a43de54a
children b2eaeeb74d87
line source
1 import Tkinter as tk
2 import tkFileDialog as tkfd
3 import engine
7 class UI(object):
8 def __init__ (self):
9 self.root = tk.Tk()
10 self.root.title("Python Battle")
11 self.canvas = tk.Canvas(self.root, background = "black")
12 self.canvas.pack(side ="top", fill="both", expand="yes")
13 buttons = tk.Frame(self.root)
14 buttons.pack(side ="bottom", fill="both", expand="yes")
15 self.buttons_pack(buttons)
16 self.step_id = 0
17 self.engine = engine.Engine(self.canvas)
18 return
19 def buttons_pack(self, frame):
20 load_1 = tk.Button(frame, text="Load", command=lambda: self.load(1))
21 load_1.pack(side="top", fill="both", expand = "yes")
22 load_2 = tk.Button(frame, text="Load", command=lambda: self.load(2))
23 load_2.pack(side="top", fill="both", expand = "yes")
24 run_b = tk.Button(frame, text="Run", command=lambda: self.run())
25 run_b.pack(side="top", fill="both", expand = "yes")
26 load_3 = tk.Button(frame, text="Load", command=lambda: self.load(3))
27 load_3.pack(side="bottom", fill="both", expand = "yes")
28 load_4 = tk.Button(frame, text="Load", command=lambda: self.load(4))
29 load_4.pack(side="bottom", fill="both", expand = "yes")
30 step_b = tk.Button(frame, text="Step", command=lambda: self.step())
31 step_b.pack(side="bottom", fill="both", expand = "yes")
32 return
34 def load (self, snake_number):
35 if self.step_id >= 200:
36 self.step_id = 0
37 pass
38 elif self.step_id == 0:
39 file_name = tkfd.askopenfilename(title="Open file")
40 snake = self.engine.create_snake(snake_number)
41 snake.load(file_name)
42 pass
43 else:
44 pass
45 return
47 def run (self):
48 self.step_id = self.id+1
49 self.engine.step()
50 self.after_id = self.canvas.after(300, self.run())
51 if self.step_id == 200:
52 self.canvas.after_cancel(self.after_id)
53 self.end()
54 pass
55 return
56 def step (self):
57 if self.id <= 200:
58 self.canvas.after_cancel(self.after_id)
59 self.id = self.id+1
60 self.engine.step()
61 pass
62 else:
63 self.end()
64 pass
65 return
67 def end (self):
68 root = tk.Tk()
69 end_label = tk.Label(root, text="End")
70 end_label.pack()
71 root.mainloop()
72 pass