Parent Child in Tkinter Treeview

Tkitner Treeview to add parent and child rows using insert() with parent and iid options

We will extend the same code taken from our basic Treeview tutorial.
Treeview parent child

To this code we will add few more rows where some rows are child of parent row. The identification of parent and child is done by the values for parent option and the iid.
The child will have to iid of parent as option.


By default the child row can be displayed by using open=True. If this value is set to False then child will be in collapsed state, however it can be displayed by expanding the Tree ( + symbol ) in tree view and by double clicking in headers view.

The full code with parent - child in Treeview is here.
from tkinter import ttk
import tkinter as tk
# Creating tkinter my_w
my_w = tk.Tk()
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode ='browse')
# number of columns
trv["columns"] = ("1", "2")
# Defining heading
trv['show'] = 'headings'
#trv['show'] = 'tree'
# width of columns and alignment 
trv.column("#0", width = 100, anchor ='c')
trv.column("1", width = 60, anchor ='c')
trv.column("2", width = 100, anchor ='c')
# Headings  
# respective columns
trv.heading("#0", text ="#")
trv.heading("1", text ="id")
trv.heading("2", text ="Name")

trv.insert("", 'end',iid='a',open=True,
               values =('a','na-Alex'))
trv.insert("", 'end',iid=1,open=True,text='1',
               values =(1,'n1-Alex'))
trv.insert("1", 'end',iid='1c',open=True,text='1c',
               values =('1c','Child-Alex'))
trv.insert("", 'end',iid=2, open=True,text=2,
               values =(2,'Ron'))
trv.insert("2", 'end',iid='2c',open=False,text='2c',
               values =('2c','Child-Ron'))
trv.insert("2c", 'end',iid='2cc', open=True,text='2cc',


Getting list of data from Treeview

for child in trv.get_children():
    #print(trv.item(child)["values"][1]) # for Name
Treeview insert user entered data
Tkinter Treeview Records of MySQL Pagination of Records MySQL Delete Records
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 All rights reserved worldwide Privacy Policy Disclaimer