-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstar_GUI.py
More file actions
538 lines (481 loc) · 20.8 KB
/
Astar_GUI.py
File metadata and controls
538 lines (481 loc) · 20.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from Tkinter import *
import tkMessageBox
import tkFont as tkfont
import ttk
import logging
from ui.world import World
from ui.world import Shape, Point, Line, Circle, Rectangle
#from astar.algorithm import Astar
from astar.algo2 import Astar
from threading import Thread
from PIL import ImageTk
from PIL import Image
FORMAT = '%(asctime)-15s %(levelname)s %(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
LOG = logging.getLogger()
"""---------------------------------------------------------------------------------------------------------------------
GUI
---------------------------------------------------------------------------------------------------------------------"""
class GUI(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
self.title('A star algorithm')
LOG.debug('Application has started!')
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# store UI frames
self.frames = {}
for F in (Top, AddShape):
page_name = F.__name__
frame = F(master=container, controller=self) # init page
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("Top")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
def get_frame(self, page_name):
return self.frames[page_name]
class Top(Frame):
def __init__(self, master, controller):
# init Frame
Frame.__init__(self, master)
# self.pack(side="top", fill="both", expand=True)
self.controller = controller
# create Frames
self.title_frame = Frame(self)
self.title_frame.grid(row=0, column=0)
self.config_frame = Frame(self)
self.config_frame.grid(row=2, column=0)
self.map_frame = Frame(self)
self.map_frame.grid(row=1, column=0)
# some variables if needed
self.world = World()
self.s = None
self.e = None
# head label
label = Label(self.title_frame, text='Run! Jerry Run!', font=controller.title_font)
label.pack(fill='x')
# config panel
self.execute_button = Button(self.config_frame, text='Play', command=self.startAlgo)
self.clear_button = Button(self.config_frame, text='Clear', command=self.clearMap)
self.init_button = Button(self.config_frame, text='Default Map', command=self.initMap)
self.setStart_button = Button(self.config_frame, text='Start Point', command=self.setStartPoint)
self.setEnd_button = Button(self.config_frame, text='End Point', command=self.setEndPoint)
self.addLine_button = Button(self.config_frame, text='Add Line', command=self.addLine)
self.addRect_button = Button(self.config_frame, text='Add Rectangle', command=self.addRect)
self.addCircle_button = Button(self.config_frame, text='Add Circle', command=self.addCircle)
self.execute_button.grid(row=0, column=0, rowspan=2, sticky=NS)
self.clear_button.grid(row=0, column=1, sticky=NSEW)
self.init_button.grid(row=1, column=1, sticky=NSEW)
self.setStart_button.grid(row=0, column=2, sticky=NSEW)
self.setEnd_button.grid(row=1, column=2, sticky=NSEW)
#self.addLine_button.grid(row=3, column=0, columnspan=2, sticky=NSEW)
self.addRect_button.grid(row=0, column=3, sticky=NSEW)
self.addCircle_button.grid(row=1, column=3, sticky=NSEW)
# config panel for drag&draw
self.area_label = Label(self.config_frame, text='Drag & Draw')
self.obstacle_button = Button(self.config_frame, text='Obstacle', command=self.area_obstacle)
self.sea_button = Button(self.config_frame, text='Sea', command=self.area_sea)
self.swamp_button = Button(self.config_frame, text='Swamp', command=self.area_swamp)
self.cancel_button = Button(self.config_frame, text='Cancel', command=self.area_cancel)
self.shape_button = Button(self.config_frame, text='Circle', command=self.shape_selection)
self.area_label.grid(row=0, column=4)
self.obstacle_button.grid(row=1, column=4, sticky=NSEW)
self.sea_button.grid(row=1, column=5, sticky=NSEW)
self.swamp_button.grid(row=1, column=6, sticky=NSEW)
self.cancel_button.grid(row=0, column=5, sticky=NSEW)
self.shape_button.grid(row=0, column=6, sticky=NSEW)
self.cancel_button['state'] = 'disabled'
# canvas
self.w = Canvas(self.map_frame, width=600, height=600, bg='white', bd=0, highlightthickness=0, relief='ridge')
self.w.pack()
self.initMap()
# bind key to start algo
self.execute_button.bind('<Return>', self.startAlgo)
# bind key for create rectangle
self.drawshape = 'rect'
self.area = None
self.rect = None
self.start_x = None
self.start_y = None
self.curX = None
self.curY = None
self.varX, self.varY = None, None
self.w.bind("<ButtonPress-1>", self.on_button_press)
self.w.bind("<B1-Motion>", self.on_move_press)
self.w.bind("<ButtonRelease-1>", self.on_button_release)
def setStartPoint(self):
frame = self.controller.get_frame('AddShape')
frame.prepare('sPoint')
self.controller.show_frame('AddShape')
return
def setEndPoint(self):
frame = self.controller.get_frame('AddShape')
frame.prepare('ePoint')
self.controller.show_frame('AddShape')
return
def addLine(self):
frame = self.controller.get_frame('AddShape')
frame.prepare('Line')
self.controller.show_frame('AddShape')
return
def addRect(self):
frame = self.controller.get_frame('AddShape')
frame.prepare('Rectangle')
self.controller.show_frame('AddShape')
return
def addCircle(self):
frame = self.controller.get_frame('AddShape')
frame.prepare('Circle')
self.controller.show_frame('AddShape')
return
def initMap(self):
# add Tom & Jerry
self.bg = ImageTk.PhotoImage(file="1.png")
self.w.create_image(0, 0, image=self.bg, anchor=NW)
image = Image.open('4.png')
self.tompic = ImageTk.PhotoImage(image)
self.tom = self.w.create_image(500-40, 100-70, image=self.tompic, anchor=NW)
image2 = Image.open('3.png')
self.jerrypic = ImageTk.PhotoImage(image2)
self.jerry = self.w.create_image(100 + 15, 500 + 10, image=self.jerrypic, anchor=NW)
# demo
self.s = self.w.create_oval(500-5, 100-5, 510, 110, fill="black")
sPoint = Point(500, 100, 'start')
self.world.start = sPoint
self.e = self.w.create_oval(100-5, 500-5, 110, 510, fill="magenta")
ePoint = Point(100, 500, 'end')
self.world.goal = ePoint
self.w.create_rectangle(150, 150, 300, 300, fill="blue")
rectangle = Rectangle(150, 150, 300, 300)
self.world.rectangles.append(rectangle)
self.world.update_map(rectangle, 'Sea')
self.w.create_rectangle(100, 400, 550, 410, fill="#808000")
rectangle = Rectangle(100, 400, 550, 410)
self.world.rectangles.append(rectangle)
self.world.update_map(rectangle, 'Swamp')
self.w.create_rectangle(300, 300, 400, 310, fill="#808080")
rectangle = Rectangle(300, 300, 400, 310)
self.world.rectangles.append(rectangle)
self.world.update_map(rectangle, 'Obstacle')
self.w.create_oval(420, 300, 470, 350, fill="red")
circle = Circle(420, 300, 470, 350)
self.world.circles.append(circle)
self.world.update_map(circle, 'Obstacle')
LOG.debug('[*]Map initialized.')
return
def clearMap(self):
self.w.delete(ALL)
self.world.init_map()
# init start & end point
self.bg = ImageTk.PhotoImage(file="1.png")
self.w.create_image(0, 0, image=self.bg, anchor=NW)
image = Image.open('4.png')
self.tompic = ImageTk.PhotoImage(image)
self.tom = self.w.create_image(500 - 40, 100 - 70, image=self.tompic, anchor=NW)
image2 = Image.open('3.png')
self.jerrypic = ImageTk.PhotoImage(image2)
self.jerry = self.w.create_image(100 + 15, 500 + 10, image=self.jerrypic, anchor=NW)
self.s = self.w.create_oval(500 - 5, 100 - 5, 510, 110, fill="black")
sPoint = Point(500, 100, 'start')
self.world.start = sPoint
self.e = self.w.create_oval(100 - 5, 500 - 5, 110, 510, fill="magenta")
ePoint = Point(100, 500, 'end')
self.world.goal = ePoint
LOG.debug('[*]Map cleared.')
return
def startAlgo(self):
LOG.debug('[*]Start Path Finding...')
self.execute_button['state'] = 'disabled'
# put algorithm into a independent thread, so that can visualize the process
t = Thread(target=self.Algo)
t.start()
return
def Algo(self):
start = (self.world.start.x, self.world.start.y)
goal = (self.world.goal.x, self.world.goal.y)
alg = Astar(self, 600, 600, start, goal)
came_from, cost_so_far = alg.a_star()
path = alg.build_path(goal, came_from, cost_so_far)
LOG.debug('[*]Path Finding Finished! Printing final path...')
self.execute_button['state'] = 'normal'
# to draw the final path
for p in path:
self.w.create_rectangle(p[0], p[1], p[0] + 1, p[1] + 1, fill='cyan')
LOG.debug('[*]Complete!')
return
def visualize_process(self, node, flag):
# draw the explored area
if flag == 'open':
self.w.create_oval(node[0], node[1], node[0] + 1, node[1] + 1, fill='cyan', outline='cyan')
else:
self.w.create_oval(node[0], node[1], node[0] + 1, node[1] + 1, fill='yellow', outline='yellow')
def on_button_press(self, e):
self.rect = None
# save mouse drag start position
self.start_x = self.w.canvasx(e.x)
self.start_y = self.w.canvasy(e.y)
# decide color
if not self.area:
color = 'red'
elif self.area == 'Obstacle':
color = '#808080'
elif self.area == 'Sea':
color = 'blue'
elif self.area == 'Swamp':
color = '#808000'
else:
color = 'white'
# create rectangle if not yet exist
if not self.rect:
if self.drawshape == 'rect':
self.rect = self.w.create_rectangle(0, 0, 1, 1, fill=color)
elif self.drawshape == 'circle':
self.rect = self.w.create_oval(0, 0, 1, 1, fill=color)
return
def on_move_press(self, e):
self.curX = self.w.canvasx(e.x)
self.curY = self.w.canvasy(e.y)
# make sure xy coordinates in the right range
if self.curX < 0:
self.curX = 0
elif self.curX > 600:
self.curX = 600
if self.curY < 0:
self.curY = 0
elif self.curY > 600:
self.curY = 600
# expand rectangle as you drag the mouse
if self.drawshape == 'rect':
self.w.coords(self.rect, int(self.start_x), int(self.start_y), int(self.curX), int(self.curY))
elif self.drawshape == 'circle':
self.varX = abs(self.curX - self.start_x)
self.varY = abs(self.curY - self.start_y)
self.w.coords(self.rect, int(self.start_x), int(self.start_y), int(self.start_x + self.varX), int(self.start_y + self.varX))
return
def on_button_release(self, e):
# check button
if not self.area:
tkMessageBox.showwarning('Input Error', 'Please select an area type first!')
self.w.delete(self.rect)
self.rect = None
return
LOG.debug('[*]Area: %s added.' % self.area)
# draw into world
if self.drawshape == 'rect':
rectangle = Rectangle(int(self.start_x), int(self.start_y), int(self.curX), int(self.curY))
self.world.rectangles.append(rectangle)
self.world.update_map(rectangle, self.area)
elif self.drawshape == 'circle':
circle = Circle(int(self.start_x), int(self.start_y), int(self.start_x + self.varX), int(self.start_y + self.varX))
self.world.circles.append(circle)
self.world.update_map(circle, self.area)
self.area = None
# enable buttons
self.cancel_button['state'] = 'disabled'
self.obstacle_button['state'] = 'normal'
self.sea_button['state'] = 'normal'
self.swamp_button['state'] = 'normal'
pass
def area_obstacle(self):
self.area = 'Obstacle'
self.cancel_button['state'] = 'normal'
self.obstacle_button['state'] = 'disabled'
self.sea_button['state'] = 'disabled'
self.swamp_button['state'] = 'disabled'
return
def area_sea(self):
self.area = 'Sea'
self.cancel_button['state'] = 'normal'
self.obstacle_button['state'] = 'disabled'
self.sea_button['state'] = 'disabled'
self.swamp_button['state'] = 'disabled'
return
def area_swamp(self):
self.area = 'Swamp'
self.cancel_button['state'] = 'normal'
self.obstacle_button['state'] = 'disabled'
self.sea_button['state'] = 'disabled'
self.swamp_button['state'] = 'disabled'
return
def area_cancel(self):
self.area = None
self.cancel_button['state'] = 'disabled'
# enable buttons
self.obstacle_button['state'] = 'normal'
self.sea_button['state'] = 'normal'
self.swamp_button['state'] = 'normal'
return
def shape_selection(self):
if self.drawshape == 'rect':
self.shape_button['text'] = 'Rect'
self.drawshape = 'circle'
LOG.debug('[*]Drawing shape change to CIRCLE.')
else:
self.shape_button['text'] = 'Circle'
self.drawshape = 'rect'
LOG.debug('[*]Drawing shape change to RECTANGLE.')
return
class AddShape(Frame):
def __init__(self, master, controller):
# init Frame
Frame.__init__(self, master)
# self.pack(side="top", fill="both", expand=True)
self.controller = controller
# shape & coordinates
self.frame = None
self.shape = None
self.x1 = None
self.y1 = None
self.x2 = None
self.y2 = None
# Labels & Entries
self.label_x1 = Label(self, text='X1')
self.label_y1 = Label(self, text='Y1')
self.label_x2 = Label(self, text='X2')
self.label_y2 = Label(self, text='Y2')
self.label_x1.grid(row=0, column=0)
self.label_y1.grid(row=1, column=0)
self.label_x2.grid(row=2, column=0)
self.label_y2.grid(row=3, column=0)
self.entry_x1 = Entry(self)
self.entry_y1 = Entry(self)
self.entry_x2 = Entry(self)
self.entry_y2 = Entry(self)
self.entry_x1.grid(row=0, column=1)
self.entry_y1.grid(row=1, column=1)
self.entry_x2.grid(row=2, column=1)
self.entry_y2.grid(row=3, column=1)
# Selection
self.area_label = Label(self, text='Type')
self.area_label.grid(row=4, column=0)
self.area = StringVar()
self.area.set('Obstacle')
self.area_option = OptionMenu(self, self.area, 'Obstacle', 'Sea', 'Swamp')
self.area_option.grid(row=4, column=1, sticky=NSEW)
# Buttons
self.submit_button = Button(self, text='Submit', command=self.drawshape)
self.back_button = Button(self, text='Back', command=self.goBack)
self.submit_button.grid(row=5, column=0)
self.back_button.grid(row=5, column=1)
def drawshape(self, e=None):
self.frame = self.controller.get_frame('Top')
w = self.frame.w
try:
area = self.area.get()
if area == 'Obstacle':
color = '#808080'
elif area == 'Sea':
color = 'blue'
elif area == 'Swamp':
color = '#808000'
else:
color = 'white'
if self.shape == 'sPoint' or self.shape == 'ePoint':
self.x1 = int(self.entry_x1.get())
self.y1 = int(self.entry_y1.get())
else:
self.x1 = int(self.entry_x1.get())
self.y1 = int(self.entry_y1.get())
self.x2 = int(self.entry_x2.get())
self.y2 = int(self.entry_y2.get())
except ValueError as e:
tkMessageBox.showwarning('Input Error', 'Please input a number!')
return
if self.shape == 'sPoint':
w.delete(self.frame.s)
w.delete(self.frame.tom)
self.frame.s = w.create_oval(self.x1, self.y1, self.x1+10, self.y1+10, fill="black")
self.frame.tom = w.create_image(self.x1 - 40, self.y1 - 70, image=self.frame.tompic, anchor=NW)
elif self.shape == 'ePoint':
w.delete(self.frame.e)
w.delete(self.frame.jerry)
self.frame.e = w.create_oval(self.x1, self.y1, self.x1 + 10, self.y1 + 10, fill="magenta")
self.frame.jerry = w.create_image(self.x1 + 15, self.y1 + 10, image=self.frame.jerrypic, anchor=NW)
elif self.shape == 'Line':
w.create_line(self.x1, self.y1, self.x2, self.y2, fill=color)
elif self.shape == 'Circle':
if self.x2-self.x1 == self.y2-self.y1:
w.create_oval(self.x1, self.y1, self.x2, self.y2, fill=color)
else:
tkMessageBox.showwarning('Input Error', 'Please value invalid! Must be a circle!')
return
elif self.shape == 'Rectangle':
w.create_rectangle(self.x1, self.y1, self.x2, self.y2, fill=color)
tkMessageBox.showinfo('Success', 'Shape added!')
self.controller.show_frame('Top')
# sync map with world
self.add2world(area)
return
def add2world(self, area):
# get world
world = self.frame.world
if self.shape == 'sPoint':
sPoint = Point(self.x1, self.y1, 'start')
world.start = sPoint
elif self.shape == 'ePoint':
ePoint = Point(self.x1, self.y1, 'end')
world.goal = ePoint
elif self.shape == 'Line': # not use any more
line = Line(self.x1, self.y1, self.x2, self.y2)
world.lines.append(line)
elif self.shape == 'Circle':
circle = Circle(self.x1-5, self.y1-5, self.x2, self.y2)
world.circles.append(circle)
world.update_map(circle, area)
elif self.shape == 'Rectangle':
rectangle = Rectangle(self.x1, self.y1, self.x2, self.y2)
world.rectangles.append(rectangle)
world.update_map(rectangle, area)
return
def prepare(self, shape):
self.clearEntry()
self.entry_x1.focus()
self.shape = shape
if shape == 'sPoint' or self.shape == 'ePoint':
self.label_x2.grid_remove()
self.label_y2.grid_remove()
self.entry_x2.grid_remove()
self.entry_y2.grid_remove()
self.entry_y1.bind('<Return>', self.drawshape)
self.area_label.grid_remove()
self.area_option.grid_remove()
else:
self.label_x2.grid()
self.label_y2.grid()
self.entry_x2.grid()
self.entry_y2.grid()
self.entry_y2.bind('<Return>', self.drawshape)
self.area_label.grid()
self.area_option.grid()
return
def goBack(self):
self.clearEntry()
self.controller.show_frame('Top')
return
def clearEntry(self):
self.entry_x1.delete(0, 'end')
self.entry_y1.delete(0, 'end')
self.entry_x2.delete(0, 'end')
self.entry_y2.delete(0, 'end')
return
"""---------------------------------------------------------------------------------------------------------------------
MAIN
---------------------------------------------------------------------------------------------------------------------"""
if __name__ == '__main__':
app = GUI()
try:
app.mainloop()
except KeyboardInterrupt:
LOG.warn('User terminated client!')