Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

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

In my TreeView I enter values manually, I would like to have them displayed like this:

Parent name1

child 1 

child 2 
   
child 3 

parent name 2 

child 1 

child 2 

The data that I enter should be added as a child to the respective parent every time I enter (the entered parent name should be checked with pre-existing parent names in treeview). Note: The data is entered into TreeView only if the failure rate is a numeric value.

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
#window
main = Tk()
main.title("tool")
main.geometry('1360x768')
count =0
#functions
def mesg():
    messagebox.showinfo("Tip", "Please Enter correct value")
def add():
    a = ie3.get()
    try:
        float(a)
        global count
        my_tree.insert(parent='', index='end',iid= count, text=ie1.get(), values=(ie2.get(), 
        ie3.get(),ie4.get(),ie5.get()))                                                                                         
        count += 1
    except ValueError:
           mesg()
#frame for Input data
input_frame = LabelFrame(main, height=370, width=1320, text='Input')
input_frame.grid(row = 0, column=0 )
# Input labels and entry boxes
i1 = Label(input_frame, text=" name:").grid(row=0, column=0,padx=5,pady=5)
ie1 = Entry(input_frame, width = 50).grid(row=0, column=1,padx=5,pady=5)
i2 = Label(input_frame, text="Fun:").grid(row=1, column=0,padx=5,pady=5)
ie2 = Entry(input_frame, width= 50).grid(row=1, column=1,padx=5,pady=5)
i3 = Label(input_frame, text="rate:").grid(row=2, column=0,padx=5,pady=5)
ie3 = Entry(input_frame, width= 50).grid(row=2, column=1,padx=5,pady=5)
i4 = Label(input_frame, text="id:").grid(row=3, column=0,padx=5,pady=5)
ie4 = Entry(input_frame, width= 50).grid(row=3, column=1,padx=5,pady=5)
i5 = Label(input_frame, text="effect").grid(row=4, column=0,padx=5,pady=5)
ie5 = Entry(input_frame, width= 50).grid(row=4, column=1,padx=5,pady=5)
#button
add_data = Button(input_frame, text ="Add data", command=add)
add_data.grid(row = 8, column = 1,padx=10,pady=10)
dele_data = Button(input_frame, text ="Delete")
dele_data.grid(row = 8, column = 2,padx=10,pady=10)
#frame for teeview
tree_frame = LabelFrame(main, height=310, width=1335, text='table', padx=5,pady=5)
tree_frame.grid(row = 1,column=0, padx=5,pady=3 )
#Scroll bar
tree_scroll = Scrollbar(tree_frame)
tree_scroll.pack(side=RIGHT, fill=Y)
#treeview
my_tree = ttk.Treeview(tree_frame,height= 13, yscrollcommand=tree_scroll.set)
my_tree.pack()
#configure scroll
tree_scroll.config(command=my_tree.yview)
#define columns
my_tree['columns'] = ("fun"," rate", "cv", "id")
#Formate column
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)
#create Heading
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()
question from:https://stackoverflow.com/questions/66052390/in-my-treeview-python-tkinter-when-i-add-data-i-want-the-data-to-be-added-ba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First all ieX are None because it is the result of .grid(...). You need to split the line like:

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

into two lines:

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

Then you need to modify add() to insert parent and child separately:

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...