Creating Adjacency table with Tkinter

53 Views Asked by At

I need to create an Adjacency table using Tkinter. I decided to use a matrix created based on the data from the vertices dictionary. The problem is when I call the create_table() function the output is incorrect.

The first row and first column are expected to be the names of the vertices and other rows and columns are expected to be filled with 1 or 0 depending on connections from the matrix, but the result isn't as expected. Any ideas how to fix this?

I am also trying to add this function which would help to update the table when user enters new vertex (a new column should be added), but not sure if it's possible with tkinter

import tkinter as tk
from tkinter import *
from tkinter import ttk

vertices = {}
matrix = []
def get_data():
    global vertices, matrix
    v = entry_vertices.get()
    c = list(entry_connections.get().split(","))
    vertices[v] = c
    for i in vertices.values():
        matrix.append(i)

    # update_table()


def create_table(vertices):
    table = ttk.Treeview(window)
    table['columns'] = list(vertices.keys())
    for vertex in vertices.keys():
        table.column(vertex, width=100)
    table.heading(vertex, text=vertex)
    for i, row in enumerate(matrix):
        table.insert('', tk.END, text=i + 1, values=row)

    table.pack()


def update_table():

    table.destroy()

window = Tk()
window.geometry("400x400")
window['bg']='LightBlue3'

label_x_coord = Label(window, text="Vertices:")
label_x_coord.pack()

entry_vertices = Entry(window)
entry_vertices.pack()

label_y_coord = Label(window, text="Connection 1(yes) 0(no):")
label_y_coord.pack()

entry_connections = Entry(window)
entry_connections.pack()


button_add = Button(window, text="Add", command=get_data)
button_add.pack()

button_create = Button(window, text="Create table", command=lambda: create_table(vertices))
button_create.pack()


window.mainloop()
0

There are 0 best solutions below