json 更新从tkinter的树视图中选择的项目

py49o6xq  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(124)

我有一个json,它在一个treeview中向我显示它的内容。我目前有一个函数,它选择项目并在框中显示它以供编辑。我的问题是如何在这个函数中调用json,以便在treeview中显示的更新在json中更新。
此函数更新树状视图中的项目

def update_record():        
    selected = json_tree.focus()
    json_tree.item(selected, text='', values=(str(logo_lb.get()),str(name_lb.get()),str(lastname_lb.get()),str(something_lb.get())))

我尝试过这种方法,它修改了treeview中的项目,但在json中,它添加了数据,而不是替换它。

selected = json_tree.focus()
    json_tree.item(selected, text='', values=(str(logo_lb.get()),str(name_lb.get()),str(lastname_lb.get()),str(something_lb.get())))
    with open('prueba1.json', "r") as f:
        data = json.load(f)
        temp = data["Example"]
        y = {
                    "Logo": str(logo_lb.get()),
                    "Name": str(name_lb.get()),
                    "Last Name": str(lastname_lb.get()),
                    "Something": str(something_lb.get()),
                    
                    }
        temp.append(y)

    with open('prueba1.json', 'w') as f:
        json.dump(data, f, indent=4)

这是一个工作原理的例子

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

ventana = tk.Tk()
ventana.title("Test")
ventana.geometry("1000x600")

frame1 = tk.Frame(ventana, bg="green", height=300, width=700)
frame1.grid(row=1, column=0)

frame2 = tk.Frame(ventana, bg="yellow", height=300, width=700)
frame2.grid(row=2, column=0)

frame_entry = tk.Frame(frame2) 
frame_entry.pack(pady=20)

tree_frame = tk.Frame(frame1) 
tree_frame.pack(pady=20)

#style del tree
style = ttk.Style()
style.theme_use("clam")
style.configure("Treeview", background="#c7c7c7", foreground="black", rowheight=25,fieldbackground="#a1a1a1")
style.map("Treeview", background=[('selected','green')])

tree_scroll = Scrollbar(tree_frame) #Frame para el scrollbar del arbol
tree_scroll.pack(side=RIGHT, fill=Y)
#Lista Treeview
json_tree = ttk.Treeview(tree_frame, yscrollcommand=tree_scroll.set)
json_tree.pack()

#config scroll
tree_scroll.config(command=json_tree.yview)

json_tree['column'] = ("Logo", "Name", "Last Name", "Something")

json_tree.column("#0", width=0, minwidth=0)
json_tree.column("Logo", anchor="w", width=120)
json_tree.column("Name", anchor="w", width=120)
json_tree.column("Last Name", anchor="w", width=120)
json_tree.column("Something", anchor="w", width=120)

#headings
json_tree.heading("#0", text="", anchor="w")
json_tree.heading("Logo", text="Logo", anchor="w")
json_tree.heading("Name", text="Name", anchor="w")
json_tree.heading("Last Name", text="Last Name", anchor="w")
json_tree.heading("Something", text="Something", anchor="w")

#color rows
json_tree.tag_configure('par', background="#fff")
json_tree.tag_configure('inpar', background="#d6d6d6")

#Open and read json

with open('prueba1.json', "r") as f:
    data = json.load(f)

    count=0
    for record in data['Example']:
        if count % 2 ==0:
            json_tree.insert(parent='', index="end", iid=count, text="", values=(record['Logo'],record['Name'],record['Last Name'],record['Something']), tags=('par',))
        else:    
            json_tree.insert(parent='', index="end", iid=count, text="", values=(record['Logo'],record['Name'],record['Last Name'],record['Something']), tags=('inpar',))

        count+=1

#entrys
l1 = Label( frame_entry, text="Logo")
l1.grid(row=0, column=0)
logo_lb = Entry( frame_entry)
logo_lb.grid(row=1, column=0)

l2 = Label( frame_entry, text="Name")
l2.grid(row=0, column=1)
name_lb = Entry(frame_entry)
name_lb.grid(row=1, column=1)

l3 = Label( frame_entry, text="Last Name")
l3.grid(row=0, column=2)
lastname_lb = Entry(frame_entry)
lastname_lb.grid(row=1, column=2)

l4 = Label( frame_entry, text="Something")
l4.grid(row=0, column=3,)
something_lb = Entry(frame_entry)
something_lb.grid(row=1, column=3)

def select_record():
    logo_lb.delete(0,END) 
    name_lb.delete(0,END)
    lastname_lb.delete(0,END)
    something_lb.delete(0,END)

    selected = json_tree.focus() 
    values = json_tree.item(selected,'values')

    logo_lb.insert(0, values[0]) 
    name_lb.insert(0,values[1])
    lastname_lb.insert(0,values[2])
    something_lb.insert(0,values[3])

select_btn = tk.Button(frame2, text="Select", command=select_record)
select_btn.pack(side=LEFT, ipadx=30,)

def update_record():        
    selected = json_tree.focus()
    json_tree.item(selected, text='', values=(str(logo_lb.get()),str(name_lb.get()),str(lastname_lb.get()),str(something_lb.get()))) 
    
    with open('prueba1.json', "r") as f:
        data = json.load(f)
        temp = data["Example"]
        y = {
                    "Logo": str(logo_lb.get()),
                    "Name": str(name_lb.get()),
                    "Last Name": str(lastname_lb.get()),
                    "Something": str(something_lb.get()),
                    
                    }
        temp.append(y)

    with open('prueba1.json', 'w') as f:
        json.dump(data, f, indent=4)
    
    logo_lb.delete(0,END) 
    name_lb.delete(0,END)
    lastname_lb.delete(0,END)
    something_lb.delete(0,END)

update_btn = tk.Button(frame2, text="Update", command=update_record)
update_btn.pack(side=RIGHT,ipadx=30, pady=10)

ventana.mainloop()

json长什么样

{
    "Example": [
        {
            "Logo": "C:/Users/*/Desktop/Tkinter/logos/selavalacarita.png",
            "Name": "2",
            "Last Name": "3",
            "Something": "4"
        },
        {
            "Logo": "C:/Users/*/Desktop/Tkinter/logos/selavalacarita.png",
            "Name": "1",
            "Last Name": "4",
            "Something": "7"
        }
    ]
}
avkwfej4

avkwfej41#

您不需要再次读取JSON文件,只需在选定项更新后将treeview的数据保存到该文件即可:

def update_record():
    selected = json_tree.focus()
    json_tree.item(selected, text='', values=(str(logo_lb.get()),str(name_lb.get()),str(lastname_lb.get()),str(something_lb.get()))) #guardar data

    # update the JSON file
    columns = ("Logo", "Name", "Last Name", "Something")
    data = [dict(zip(columns,json_tree.item(iid,"values"))) for iid in json_tree.get_children()]
    with open("prueba1.json", "w") as f:
        json.dump({"Example":data}, f, indent=4)

    #Limpiar las cajas
    logo_lb.delete(0, END)
    name_lb.delete(0, END)
    lastname_lb.delete(0, END)
    something_lb.delete(0, END)

我建议使用数据库(如SQLite)而不是JSON文件来存储这些记录。

相关问题