We can use insert() to add one parent of main node ( row ) to the Treeview. Here we have one student record table ( no database here ) and one record is already available. Below the Treeview, input boxes and options are available to add nodes to this Treeview using insert() method.
Declaring a Treeview
Here we have declared one Treeview and use the object trv for further management of the Treeview.
parent : For Parent row it should be '', for child row parent iid to be used. index : Index position of row. To added at the bottom 'end' and 0 if at top. iid : Optional, Identifier of the item ( row ), Treeview will create unique iid if not given. text : Text to appear at right of icon column.
Using this after adding header and columns ( check the code below ) one record is added with iid=1 ( unique id of the row )
Below this Treeview we will provide inputs using Label, Text, OptionMenu and Radio buttons to take one input row data and add to the row on Click of a Button.
The function add_data() will be executed once the Buttonb1 is clicked. Inside this function we will read the user entered data and then add the row to our Treeview. Here we are not validating any input data.
def add_data():
my_name=t1.get("1.0",END) # read name
my_class=options.get() # read class
my_mark=t3.get("1.0",END) # read mark
my_gender=radio_v.get() # read gender
global i
i=i+1
trv.insert("",'end',values=(i,my_name,my_class,my_mark,my_gender))
t1.delete('1.0',END) # reset the text entry box
t3.delete('1.0',END) # reset the text entry box
my_str.set("Data added ")
t1.focus()
l5.after(3000, lambda: my_str.set('') ) # remove the message