在我的树视图(python tkinter)中,当我添加数据时,我希望根据父名称(tkinter)添加数据

In my treeview (python tkinter), when I add data, I want the data to be added based on parent name (tkinter)

在我的 TreeView 中,我手动输入值,我希望它们像这样显示:

Parent name1

child 1 

child 2 
   
child 3 

parent name 2 

child 1 

child 2 

每次输入时,我输入的数据都应作为子项添加到相应的父项(输入的父项名称应与树视图中预先存在的父项名称进行检查)。 注意:只有故障率是数值时才将数据录入TreeView。

#treeview
my_tree = ttk.Treeview(tree_frame,height= 15, yscrollcommand=tree_scroll.set)
my_tree.pack()

tree_scroll.config(command=my_tree.yview)

my_tree['columns'] = ("fun"," rate", "cv", "id")

my_tree.column("#0",width=100)
my_tree.column("fun",anchor=W,width=100)
my_tree.column("rate",anchor=W,width=100)
my_tree.column("cv",anchor=W,width=120)
my_tree.column("id",anchor=W,width=120)

my_tree.heading("#0",text='name',anchor=W)
my_tree.heading("fun", text='Fun', anchor=W)
my_tree.heading("rate",text='rate',anchor=W)
my_tree.heading("cv", text='Cv',anchor=W)
my_tree.heading("id",text='id',anchor=W)

main.mainloop()

首先所有ieX都是None因为它是.grid(...)的结果。您需要像这样拆分行:

ie1 = Entry(input_frame, width = 50).grid(row=0, column=1,padx=5,pady=5)

分为两行:

ie1 = Entry(input_frame, width = 50)
ie1.grid(row=0, column=1,padx=5,pady=5)

然后需要修改add(),分别插入parentchild

def add():
    global count
    a = ie3.get()
    try:
        float(a)
        component = ie1.get()
        if not my_tree.exists(component):
            # insert parent
            my_tree.insert(parent="", index="end", iid=component, text=component)
        # insert child
        my_tree.insert(parent=component, index='end', iid=count, values=(ie2.get(),ie3.get(),ie4.get(),ie5.get()))                                                                                         
        count += 1
    except ValueError:
        mesg()