import tkinter as tk
from tkinter import ttk
import json

FILENAME = 'products.json'

def get_idx():
    return len(tree.get_children()) + 1

def add_new_product():
    tree.insert('', 'end', values=(get_idx(), entry_product.get()))
    entry_product.delete(0, tk.END)

def save_to_json():
    data = []

    for child in tree.get_children():
        data.append(tree.item(child)["values"])

    with open(FILENAME, "w", encoding="utf-8") as file:
        json.dump(data, file, indent=4)

def read_from_json():
    with open(FILENAME, "r", encoding="utf-8") as f:
        data = json.load(f)

    for i in tree.get_children():
        tree.insert('', )

window = tk.Tk()
window.title('Lista zakupów')
window.geometry('400x500')

# wprowadzenie danych
entry_product = tk.Entry(window, width=40)
entry_product.pack(pady=10)

# przyciski
product_add = tk.Button(window, text="Dodaj produkt", command=read_from_json)
product_add.pack(pady=5)

product_save = tk.Button(window, text="Zapisz produkt", command=save_to_json)
product_save.pack(pady=5)

tree = ttk.Treeview(window, columns=('id', 'product'), show='headings', height=10)
tree.heading('id', text='idx')
tree.heading('product', text='produkt')

tree.column('id', anchor='center')
tree.column('product', anchor='center')
tree.pack(pady=5)

window.mainloop()