Parent Child in Tkinter Treeview

Treeview
  • Tkinter Treeview understanding Parent - Child


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.

open=True/False

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()
my_w.geometry("400x280") 
my_w.title("www.plus2net.com")  
# Using treeview widget
trv = ttk.Treeview(my_w, selectmode ='browse')
  
trv.grid(row=1,column=1,padx=20,pady=20)
# number of columns
trv["columns"] = ("1", "2")
  
# Defining heading
trv['show'] = 'headings'
#trv['show'] = 'tree'
s=ttk.Style()
s.configure(trv,rowhieght=20)
s.configure(trv,height=20)
# 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',
           values=('2cc','Child-2-Ron'))

my_w.mainloop()


Treeview insert user entered data
Tkinter Treeview Records of MySQL Pagination of Records MySQL Delete Records


plus2net.com



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-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer