Tkinter Treeview widget .
Tkinter ttk Treeview .
Output .
treeview — hierarchical multicolumn data display widget
The ttk::treeview widget displays a hierarchical collection of items. Each item has a textual label, an optional image, and an optional list of data values. The data values are displayed in successive columns after the tree label.
The tree widget can also display column headings.
Tree View is also called as data grid in some Frame work
In this Blog you will learn how to create a treeview , how to insert and delete to and from it and much more .
So lets Create a simple Tree view.
# Tkinter - Dynamic Coding
# treeview .
# Import Library ......
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Dynamic Coding - TreeView")
columns = ("name", "surname","id")
treeview = ttk.Treeview(root, height=5,
show="headings", columns=columns)
# add column properties
treeview.column("name", width=100, anchor='center')
treeview.column("surname", width=100, anchor='center')
treeview.column("id",width=40,anchor = 'center')
# adding heading
treeview.heading("name", text="Name") # Show header
treeview.heading("surname", text="Surname")
treeview.heading("id",text = "I'd")
treeview.pack(side=TOP, fill=BOTH)
# Data ti be Inserted into Treeview
Name = ['Yogesh', 'Alex', 'Akshat','shubham','rohan']
sur = ['Singh','Roy','Rajput','raj','Sharma']
ids = ['100','101','102','103','104']
for i in range(min(len(Name),len(sur),len(ids))):
treeview.insert('', i, values=(Name[i], sur[i],ids[i]))
# to insert data we use Insert method
root.mainloop()
Output .
Fetch and delete Data from Treeview.
# Tkinter - Dynamic Coding
# treeview .
# Import Library ......
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Dynamic Coding - TreeView")
def func2():
item = treeview.selection()
for i in item:
print("you clicked on", treeview.item(i, "values")[0])
# only prints the Name because the index passed is 0 .
def delete1():
item = treeview.selection()
treeview.delete(item)
columns = ("name", "surname","id")
treeview = ttk.Treeview(root, height=5, show="headings", columns=columns)
treeview.column("name", width=100, anchor='center')
treeview.column("surname", width=100, anchor='center')
treeview.column("id",width=40,anchor = 'center')
treeview.heading("name", text="Name") # Show header
treeview.heading("surname", text="Surname")
treeview.heading("id",text = "I'd")
treeview.pack(side=TOP, fill=BOTH)
Name = ['Yogesh', 'Alex', 'Akshat','shubham','rohan']
sur = ['Singh','Roy','Rajput','raj','Sharma']
ids = ['100','101','102','103','104']
for i in range(min(len(Name),len(sur),len(ids))):
treeview.insert('', i, values=(Name[i], sur[i],ids[i]))
Button(root,text = 'get selected data',command=func2).pack(pady=10,padx=10,side=LEFT)
Button(root,text = 'delete selected data',command=delete1).pack(pady=10,padx=10,side=RIGHT)
root.mainloop()
Output.
Add styling to Treeview Content / Data.
# Tkinter - Dynamic Coding
# treeview .
# Import Library ......
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Dynamic Coding - TreeView")
def func2():
item = treeview.selection()
for i in item:
print("you clicked on", treeview.item(i, "values")[0])
def delete1():
item = treeview.selection()
treeview.delete(item)
columns = ("name", "surname","id")
treeview = ttk.Treeview(root, height=5, show="headings", columns=columns)
treeview.column("name", width=100, anchor='center')
treeview.column("surname", width=100, anchor='center')
treeview.column("id",width=40,anchor = 'center')
treeview.heading("name", text="Name") # Show header
treeview.heading("surname", text="Surname")
treeview.heading("id",text = "I'd")
treeview.pack(side=TOP, fill=BOTH)
# configure .......
Name = ['Yogesh', 'Alex', 'Akshat','shubham','rohan']
sur = ['Singh','Roy','Rajput','raj','Sharma']
ids = ['100','101','102','103','104']
for i in range(min(len(Name),len(sur),len(ids))):
if i % 2 ==0 :
col ='gray'
else:
col = 'white'
treeview.insert('', i, values=(Name[i], sur[i],ids[i]),tag=col)
# tag_configure method is used to cinfigure tag ..
treeview.tag_configure('gray', background='#cccccc')
treeview.tag_configure('white', background='#ffffff')
Button(root,text = 'get selected data',command=func2).pack(pady=10,padx=10,side=LEFT)
Button(root,text = 'delete selected data',command=delete1).pack(pady=10,padx=10,side=RIGHT)
root.mainloop()
Bind Mouse Click to Treeview.
# Tkinter - Dynamic Coding
# treeview .
# Import Library ......
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Dynamic Coding - TreeView")
def func(event):
item = treeview.selection()
#print(item)
for i in item:
print("you clicked on", treeview.item(i, "values")[0])
def func2():
item = treeview.selection()
for i in item:
print("you clicked on", treeview.item(i, "values")[0])
def delete1():
item = treeview.selection()
treeview.delete(item)
columns = ("name", "surname","id")
treeview = ttk.Treeview(root, height=5, show="headings", columns=columns)
treeview.column("name", width=100, anchor='center')
treeview.column("surname", width=100, anchor='center')
treeview.column("id",width=40,anchor = 'center')
treeview.heading("name", text="Name") # Show header
treeview.heading("surname", text="Surname")
treeview.heading("id",text = "I'd")
treeview.pack(side=TOP, fill=BOTH)
# configure .......
Name = ['Yogesh', 'Alex', 'Akshat','shubham','rohan']
sur = ['Singh','Roy','Rajput','raj','Sharma']
ids = ['100','101','102','103','104']
for i in range(min(len(Name),len(sur),len(ids))):
if i % 2 ==0 :
col ='gray'
else:
col = 'white'
treeview.insert('', i, values=(Name[i], sur[i],ids[i]),tag=col)
treeview.tag_configure('gray', background='#cccccc')
treeview.tag_configure('white', background='#ffffff')
# for single click ....
treeview.bind("<<TreeviewSelect>>>", func)
#treeview.bind("<Double-1>", func) # if you need double click ..
Button(root,text = 'get selected data',command=func2).pack(pady=10,padx=10,side=LEFT)
Button(root,text = 'delete selected data',command=delete1).pack(pady=10,padx=10,side=RIGHT)
root.mainloop()
Recommended Posts:
Thank you ..
If have Any doubts and Errors , comment section is open for you and we will try to solve your doubts as soon as possible.
No comments