Пользователь вводит в текстовые поля названия точек.
На холсте прорисовываются все возможные пути, по которым из первой точки можно попасть во вторую.
# Загрузка данных из файлов
points = {}
for line in open('points.txt'):
line = line.split('\n')
line = line[0]
line = line.split(' ')
line[1] = int(line[1])
line[2] = int(line[2])
points[line[0]] = line[1:]
graphs = {}
for line in open('graphs.txt'):
line = line.split('\n')
line = line[0]
line = line.split(' ')
graphs[line[0]] = line[1:]
#############################
from tkinter import *
window = Tk()
canv = Canvas(window,width=500,height=500,bg="white")
ent1 = Entry(window,width=2)
ent2 = Entry(window,width=2)
but = Button(window,text="Paths")
def func_points(): # прорисовка точек
for i in points:
coords = points[i]
x = coords[0]
y = coords[1]
canv.create_rectangle(x,y,x+5,y+5,fill="black")
canv.create_text(x-5,y-5,text=i)
def check_graph(graph,first_p,second_p,col,indent): #обработка каждого графа-маршрута
colors = ["green","red","blue"]
qty = 0 # Количество точек между заданными пользователем точками
if first_p in graph:
if second_p in graph:
f = 0 # Где находится первая
while first_p != graph[f]: # определяем
f += 1
s = 0 # Где находится вторая точка
while second_p != graph[s]:
s += 1
if f < s: # Если первая точка встречается раньше, чем вторая:
qty = s - f # количество точек от первой до второй
while f < s: # прорисовка отрезков
coords = points[graph[f]]
x1 = coords[0]
y1 = coords[1]
coords = points[graph[f+1]]
x2 = coords[0]
y2 = coords[1]
canv.create_line(x1+indent,y1+indent,\
x2+indent,y2+indent,fill=colors[col])
f += 1
else: # Если вторая точка встречается раньше, чем первая
qty = f - s
while s < f:
coords = points[graph[f]]
x1 = coords[0]
y1 = coords[1]
coords = points[graph[f-1]]
x2 = coords[0]
y2 = coords[1]
canv.create_line(x1+indent,y1+indent,\
x2+indent,y2+indent,fill=colors[col])
f -= 1
col += 1
indent += 2
return qty,col,indent
def func_paths(event):
canv.delete('all')
func_points()
col = 0 # счетчик для цвета
indent = 0 # отступ
first_p = ent1.get()
second_p = ent2.get()
qty_points = 100
min_graph = ' '
for i in graphs:
q,col,indent = check_graph\
(graphs[i],first_p,second_p,col,indent)
if q != 0 and q < qty_points:
qty_points = q
min_graph = i
print('The shortest path:',min_graph)
func_points()
but.bind("",func_paths)
canv.pack()
ent1.pack()
ent2.pack()
but.pack()
window.mainloop()