-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
373 lines (271 loc) · 10.9 KB
/
main.py
File metadata and controls
373 lines (271 loc) · 10.9 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
import os
import heapq
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont
from parser import ParseFile
from math import sqrt
# markers are "location pins" on the map
class Marker():
def __init__(self, coords, name=None, color="Red"):
self.coords = coords
self.name = name
self.color = color
# subclass of Marker with default color as blue
class LocationMarker(Marker):
def __init__(self, coords, name=None, color="blue"):
super().__init__(coords, name, color)
# subclass of Marker with default color as green
class PathMarker(Marker):
def __init__(self, coords, name=None, color="red"):
super().__init__(coords, name, color)
def CloseWindow(event=None):
root.destroy()
# draws the markers on the canvas provided
def DrawMarkers(markers, canvas):
# checks if the markers is list of markers or a single marker
if isinstance(markers, list):
for marker in markers:
if isinstance(marker, list):
for i in marker:
x, y = i.coords
canvas.create_rectangle(x, y, x + 5, y + 5, fill=i.color)
else:
x, y = marker.coords
canvas.create_rectangle(x, y, x + 5, y + 5, fill=marker.color)
else:
x, y = markers.coords
canvas.create_rectangle(x, y, x + 5, y + 5, fill=markers.color)
# creates and returns a canvas of entered background color
def CreateMap(root, canvasBackground):
canvas = tk.Canvas(right, bg=canvasBackground, width=850, height=800, highlightthickness=0)
canvas.grid(row=0, column=0, sticky="")
right.grid_rowconfigure(0, weight=1)
right.grid_columnconfigure(0, weight=1)
return canvas
# returns location and path markers from the coords entered in the coords.txt file
def GetMarkersFromFile(file):
locationMarkers = []
pathMarkers = []
locationMarkerCoords, locationMarkersNames, pathMarkerCoords = ParseFile(file)
for i in range(len(locationMarkerCoords)):
locationMarkers.append(LocationMarker(locationMarkerCoords[i], name=locationMarkersNames[i]))
for i in pathMarkerCoords:
temp = []
for j in i:
temp.append(PathMarker(j))
pathMarkers.append(temp)
return locationMarkers, pathMarkers
# uses Djikstra's algo to find the shortest path between two coords from the graph
def GetPathLength(graph, start_coords, end_coords):
# Priority queue to store (current distance, node) tuples
to_visit = [(0, start_coords)]
# Dictionary to keep track of the shortest distance to each node
shortest_distances = {start_coords: 0}
# Dictionary to keep track of the predecessor of each node in the shortest path
predecessors = {}
while to_visit:
# Pop the node with the smallest distance from the queue
current_dist, current_node = heapq.heappop(to_visit)
# If we reach the destination node, reconstruct the path
if current_node == end_coords:
path = []
while current_node is not None:
path.append(current_node)
current_node = predecessors.get(current_node)
path.reverse()
return current_dist, path
# Explore neighbors of the current node
for neighbor, length in graph[current_node].items():
# Calculate the distance to this neighbor
new_dist = current_dist + length
# If the calculated distance is shorter, update the shortest distance and add to the queue
if neighbor not in shortest_distances or new_dist < shortest_distances[neighbor]:
shortest_distances[neighbor] = new_dist
predecessors[neighbor] = current_node
heapq.heappush(to_visit, (new_dist, neighbor))
return None, None # If there's no path
# returns the straight line distance between two coords
def GetDistance(coords1, coords2):
x1, y1 = coords1
x2, y2 = coords2
return sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
# converts the path markers into a graph format
def BuildGraph(pathMarkerList):
graph = {}
for path in pathMarkerList:
for i in range(len(path) - 1):
coords1 = path[i].coords
coords2 = path[i+1].coords
distance = round(GetDistance(coords1, coords2), 2)
if coords1 not in graph:
graph[coords1] = {}
if coords2 not in graph:
graph[coords2] = {}
graph[coords1][coords2] = distance
graph[coords2][coords1] = distance
return graph
# highlights the entered path
def HighlightPath(path):
for i in range(len(path) - 1):
map.create_line(
path[i], path[i + 1], fill="blue", width=3)
# unhighlights the entered path
def UnhighlightPreviousPath(path):
for i in range(len(path) - 1):
map.create_line(
path[i], path[i + 1], fill="black", width=3)
# Returns the coords of a marker from its name
def GetCoordsFromName(name):
global locationMarkersList
for i in locationMarkersList:
if i.name.strip() == name:
return i.coords
# update map with the new coords
def UpdateMap(a, b):
global previousPath
UnhighlightPreviousPath(previousPath)
path = []
distance, path = GetPathLength(graph, GetCoordsFromName(a), GetCoordsFromName(b))
previousPath = path
HighlightPath(path)
# root window
root = tk.Tk("Nav_sys")
root.geometry("850x800")
root.title("Campus Navigation System")
root.state("zoomed")
# root.attributes("-fullscreen", True)
# paths handling using os
cwd = os.getcwd()
file = os.path.join(cwd, "coords.txt")
backgroundImagePath = os.path.join(cwd, "map.png")
backgroundImage = tk.PhotoImage(file = backgroundImagePath)
#*---------------------------------------------------------------------------------------------------------------------------
#Custom font
cusfont = tkfont.Font(family="Aptos", size=14)
# Container frame
main_frame = ttk.Frame(root)
main_frame.pack(fill="both", expand=True, side="left")
# Main grid configuration
main_frame.grid_columnconfigure(0, weight=1)
main_frame.grid_columnconfigure(1, weight=1)
main_frame.grid_rowconfigure(0, weight=1)
# Left frame in main frame
left = tk.Frame(main_frame, width=300, height=800, bg="black")
left.grid(row=0, column=0, sticky="nswe")
# Right frame in main frame
right = tk.Frame(main_frame, width=850, height=800, bg="#3e3f43")
right.grid(row=0, column=1, sticky="nswe")
# Dropdown menu
def dropdown():
global ask, sel, menu, confirmbutton, placeholder
global ask0, sel0, menu0, placeholder0
#starting point menu
options = ["AB1", "AB2", "AB3", "Library", "Canteen", "Main Office", "Post Office"]
sel = tk.StringVar()
sel0 = tk.StringVar()
ask0 = ttk.Label(left, text="Select Starting point", font=cusfont, background="black", foreground="white")
ask0.place(relx=0.5, rely=0.2, anchor="center")
menu0 = ttk.Combobox(left, values=options, textvariable=sel0, font=cusfont,state="readonly")
menu0.place(relx=0.5, rely=0.3, anchor="center")
placeholder0 = ttk.Label(left, text="Pick an option below", font=cusfont, foreground="gray")
placeholder0.place(relx=0.5, rely=0.3, anchor="center")
def on_select0(event):
placeholder0.place_forget()
menu0.bind("<<ComboboxSelected>>", on_select0)
#destination menu
ask = ttk.Label(left, text="Select destination", font=cusfont, background="black", foreground="white")
ask.place(relx=0.5, rely=0.4, anchor="center")
menu = ttk.Combobox(left, values=options, textvariable=sel, font=cusfont,state="readonly")
menu.place(relx=0.5, rely=0.5, anchor="center")
placeholder = ttk.Label(left, text="Pick an option below", font=cusfont, foreground="gray")
placeholder.place(relx=0.5, rely=0.5, anchor="center")
def on_select(event):
placeholder.place_forget()
menu.bind("<<ComboboxSelected>>", on_select)
#confirm button
confirmbutton = ttk.Button(left, text="Confirm selection", command=clconfirmbutton)
confirmbutton.place(relx=0.5, rely=0.6, anchor="center")
# Creating home button
def crhomebutton():
global backbutton
backbutton = ttk.Button(left, text="Home", command=clbackbutton)
backbutton.place(relx=0.5, rely=0.9, anchor="center")
# Clicking home button
def clbackbutton():
resetui()
dropdown()
crhistorrybutton()
crhomebutton()
# Creating history button
def crhistorrybutton():
global historybutton
historybutton = ttk.Button(left, text="View history", command=clhistorybutton)
historybutton.place(relx=0.5, rely=0.7, anchor="center")
# Clicking history button
def clhistorybutton():
global log
resetui()
try:
with open("history.txt", "r") as file:
history = file.read().strip() or "No results"
except FileNotFoundError:
history = "No results"
log = ttk.Label(left, text=history, font=cusfont, background="black", foreground="white")
log.place(relx=0.5, rely=0.5, anchor="center")
crclrhistorybutton()
crhomebutton()
# Creating clear history button
def crclrhistorybutton():
global clrhistorybutton
clrhistorybutton = ttk.Button(left, text="Clear history", command=clclrhistorybutton)
clrhistorybutton.place(relx=0.5, rely=0.7, anchor="center")
# Clicking clear history button
def clclrhistorybutton():
with open("history.txt", "w") as file:
pass
log.config(text="History cleared")
clrhistorybutton.destroy()
selopt = None
startopt = None
# Clicking confirm button
def clconfirmbutton():
global selopt, startopt
selopt = sel.get()
startopt = sel0.get()
if not selopt:
disp_text = "No option selected for destination"
elif not startopt:
disp_text = "No option selected for starting point"
else:
disp_text = f"Showing route From {startopt} to {selopt}"
with open("history.txt", "a") as file:
file.write(f"{startopt} -> {selopt}\n")
resetui()
if not (startopt == "" or selopt == ""):
UpdateMap(startopt, selopt)
global disp
disp = ttk.Label(left, text=disp_text, font=cusfont, background="black", foreground="white")
disp.place(relx=0.5, rely=0.4, anchor="center")
crhomebutton()
crhistorrybutton()
# Reset user interface
def resetui():
for widget in left.winfo_children():
widget.destroy()
#Start function
def start():
dropdown()
crhomebutton()
crhistorrybutton()
#*---------------------------------------------------------------------------------------------------------------------------
map = CreateMap(root, "black")
map.create_image(0, 0, anchor="nw", image=backgroundImage)
locationMarkersList, pathMarkerList = GetMarkersFromFile(file)
graph = BuildGraph(pathMarkerList)
previousPath = []
# DrawMarkers(locationMarkersList, map)
# DrawMarkers(pathMarkerList, map)
root.bind("<Escape>", CloseWindow)
start()
root.mainloop()