I need to make a graph based on created table (user enters the vertex name, X and Y coordinates, distance column remains empty at this stage). After the data is added into table I need minimal distance to each of the vertexes be calculated and inserted into distance column. This is the code I have:
from tkinter import ttk
import networkx as nx
import matplotlib.pyplot as plt
from tkinter import *
from tkinter import ttk
# Creating table
def create_table():
global table
table = ttk.Treeview(window)
# Creating columns
table["columns"] = ("name", "x_coord", "y_coord", "distance")
table.column("#0", width=100, minwidth=100)
table.column("name", width=100, minwidth=100)
table.column("x_coord", width=100, minwidth=100)
table.column("y_coord", width=100, minwidth=100)
table.column("distance", width=100, minwidth=100)
table.heading("#0", text="Vertex")
table.heading("name", text="Vertex name")
table.heading("x_coord", text="X cord")
table.heading("y_coord", text="Y cord")
table.heading("distance", text="Min distance")
# Отображение таблицы
table.pack()
def add_data():
name = entry_name.get()
x_coord = entry_x_coord.get()
y_coord = entry_y_coord.get()
table.insert("", "end", text=str(len(table.get_children()) + 1), values=(name, x_coord, y_coord, int(len(table.get_children()) + 1), ""))
window = Tk()
window.geometry("400x400")
# create_table()
label_name = Label(window, text="Vertex name:")
label_name.pack()
entry_name = Entry(window)
entry_name.pack()
label_x_coord = Label(window, text="X cord:")
label_x_coord.pack()
entry_x_coord = Entry(window)
entry_x_coord.pack()
label_y_coord = Label(window, text="Y cord:")
label_y_coord.pack()
entry_y_coord = Entry(window)
entry_y_coord.pack()
button_add = Button(window, text="Add", command=add_data)
button_add.pack()
def calculate_distances(vertices):
distances = {}
for i in range(len(vertices)):
for j in range(i + 1, len(vertices)):
distance = ((vertices[i][1] - vertices[j][1]) ** 2 + (vertices[i][2] - vertices[j][2]) ** 2) ** 0.5
distances[(vertices[i][0], vertices[j][0])] = distance
distances[(vertices[j][0], vertices[i][0])] = distance
# table.set(f"{i+1}", "distance", str(distance))
# table.set(f"{j+1}", "distance", str(distance))
return distances
def build_graph():
# getting data from the table
vertices = []
for child in table.get_children():
values = table.item(child)['values']
vertices.append((values[0], float(values[1]), float(values[2])))
# calculating distances
distances = calculate_distances(vertices)
# making graph
G = nx.Graph()
for vertex in vertices:
G.add_node(vertex[0])
for edge, distance in distances.items():
G.add_edge(edge[0], edge[1], weight=round(distance, 2))
# graph visualization
pos = {}
for vertex in vertices:
pos[vertex[0]] = (vertex[1], vertex[2])
nx.draw(G, pos, with_labels=True, node_size=700, node_color='lightblue', font_weight='bold', font_size=10,
edge_color='grey', width=2, edge_cmap=plt.cm.Blues)
labels = nx.get_edge_attributes(G, 'weight')
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
plt.show()
window = tk.Tk()
# creating table
create_table()
build_graph_button = tk.Button(window, text="Построить граф", command=build_graph)
build_graph_button.pack()
window.mainloop()
As you can see in the code I tried filling distance column with numbers and then replace it with distance value using table.set, however I kept getting this error: Item 1 is not found. Any ideas how to fix this and insert the minimal distance for each vertex into table?