-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvsgui.py
166 lines (154 loc) · 6.51 KB
/
cvsgui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding:utf-8 -*-
#Simple Cal
#######################
from Tkinter import *
import Tkinter
from operator import add
def frame(root, side):
w = Frame(root)
w.pack(side=side, expand=YES, fill=BOTH)
return w
#end of def
def button(root, side, text = None, textvariable= None, command=None):
w = Button(root, text=text, textvariable=textvariable, command=command)
w.pack(side=side, expand=YES, fill=BOTH)
return w
#end of def
class cvsBoard(Frame):
def __init__(self,cvsMap,map_nr):
Frame.__init__(self)
self.cvsMap = cvsMap
self.map_nr = map_nr
self.master.geometry('+500+300')
self.option_add('*Font', 'Courier 8 bold')
self.pack(expand=YES, fill=BOTH)
self.master.title('Board')
self.master.iconname('board')
display = StringVar()
self.display = display
Entry(self, relief=SUNKEN, textvariable=display).pack(side=TOP, expand=YES, fill=BOTH)
self.btnTexts = []
for i in xrange(cvsMap.h):
rowF = frame(self,TOP)
btnTextli = []
for j in xrange(cvsMap.w):
v = StringVar()
v.set(self.getStr(i,j))
btnTextli.append(v)
btn = button(rowF,LEFT,textvariable = v)
btn.bind('<ButtonRelease-1>',lambda e,i=i,j=j: self.setPos(i,j))
btn.bind('<ButtonRelease-2>',lambda e,i=i,j=j: self.blockPos(i,j))
btn.bind('<ButtonRelease-3>',lambda e,i=i,j=j: self.clearPos(i,j))
self.btnTexts.append(btnTextli)
#end of for
#end of for
ioF = frame(self,TOP)
button(ioF, LEFT, text = 'Evaluate!', command = lambda display = display:self.getEva(display))
button(ioF, LEFT, text = 'Reset!', command = self.Reset)
button(ioF, LEFT, text = 'OutPut!', command = self.getResult)
#end of def
def getEva(self,display):
from vs_astar import a_star
from vs_astar import getPath
havePath = False
pathli = []
for g in self.cvsMap.gli:
for s in self.cvsMap.sli:
r = a_star(s, g, cvsMap)
if r == -1:
continue
havePath = True
pathli.append(getPath(r))
# end of for
# end of for
if not havePath:
display.set("ERROR: NO PATH")
else:
display.set(reduce(add, (len(x) for x in pathli)))
# show the path on map
for path in pathli:
for pos in path:
self.btnTexts[pos[0]][pos[1]].set(' P ')
# end of def
def Reset(self):
for i in xrange(cvsMap.h):
for j in xrange(cvsMap.w):
self.btnTexts[i][j].set(self.getStr(i, j))
def getStr(self,i,j):
if self.cvsMap.mmap[i][j] == '0':
return ' '
if self.cvsMap.mmap[i][j] == 't':
tower = cvsMap.tdict[(i,j)]
return 't:'+str(tower.stren) + ',' + str(tower.t)
if self.cvsMap.mmap[i][j] == 's':
return 'sssss'
if self.cvsMap.mmap[i][j] == 'g':
return 'ggggg'
if self.cvsMap.mmap[i][j] == '1':
return 'X X X'
else:
return self.cvsMap.mmap[i][j]+' '
def blockPos(self,i,j):
from solve import Tower
#print 'INFO:',i,j
if self.cvsMap.mmap[i][j] == '0':
cvsMap.addTower(Tower(i,j,0,0))
self.btnTexts[i][j].set(self.getStr(i, j)) # reset string
pass
def clearPos(self,i,j):
if self.cvsMap.mmap[i][j] == 't':
cvsMap.delTower(i,j)
self.btnTexts[i][j].set(self.getStr(i, j)) # reset string
def setPos(self,i,j):
from solve import Tower
if self.cvsMap.mmap[i][j] == 't':
cvsMap.delTower(i,j)
self.btnTexts[i][j].set(self.getStr(i, j)) # reset string
elif self.cvsMap.mmap[i][j] == '0':
d = MyDialog(self,i,j) # 生成对话框
self.wait_window(d.top) # 等待对话框结束
try:
#tkMessageBox.showinfo('Python','You input stren,t:\n' + d.get())
inputs = d.get().split(',');
except Exception:
return
cvsMap.addTower(Tower(i,
j,
int(inputs[0]),
int(inputs[1])))
self.btnTexts[i][j].set(self.getStr(i, j)) # reset string
else:
pass
#end of if
#end of def
def getResult(self):
#self.destroy()
self.quit()
print 'Here!'
return cvsMap # will it get here?
pass
#end of def
#end of class
class MyDialog: # 定义对话框类
def __init__(self, root,i,j): # 对话框初始化
self.top = Tkinter.Toplevel(root) # 生成Toplevel组件
label = Tkinter.Label(self.top, text='Please Input tower for %d,%d'%(i,j)) # 生成标签组件
label.pack()
self.entry = Tkinter.Entry(self.top) # 生成文本框组件
self.entry.pack()
self.entry.focus() # 让文本框获得焦点
btn = Tkinter.Button(self.top, text='Ok', # 生成按钮
command=self.Ok) # 设置按钮事件处理函数
self.top.bind('<Return>',lambda e:self.Ok(),'+')
self.top.geometry('300x100+500+200')
btn.pack()
def Ok(self): # 定义按钮事件处理函数
self.input = self.entry.get() # 获取文本框中内容,保存为input
self.top.destroy() # 销毁对话框
def get(self): # 返回在文本框输入的内容
return self.input
if __name__ == '__main__':
from solve import readMapInput
s = int(raw_input())
cvsMap,l = readMapInput()
cvsBoard(cvsMap,10).mainloop()