Hierarchical treeview in Python GUI application

Python uses different GUI applications that are helpful for the users while interacting with the applications they are using. There are basically three GUI(s) that python uses namely Tkinter, wxPython, and PyQt. All of these can operate with windows, Linux, and mac-OS. However, these GUI applications have many widgets i.e, controls that are helpful for the user interaction with the application. Some of the widgets are buttons, list boxes, scrollbar, treeview, etc.

Note: For more information, refer to Python GUI – tkinter

Treeview widgets

This widget is helpful in visualizing and permitting navigation over a hierarchy of items. It can display more than one feature of every item in the hierarchy. It can build tree view like user interface like in windows explorer. Therefore, here we will use Tkinter in order to construct a hierarchical treeview in Python GUI application.
Lets see an example of constructing a hierarchical treeview in Python GUI application.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to illustrate the usage
# of hierarchical treeview in python GUI
# application using tkinter
  
# Importing tkinter
from tkinter import *  
  
# Importing ttk from tkinter
from tkinter import ttk  
  
# Creating app window
app = Tk()  
  
# Defining title of the app
app.title("GUI Application of Python")  
  
# Defining label of the app and calling a geometry
# management method i.e, pack in order to organize
# widgets in form of blocks before locating them 
# in the parent widget
ttk.Label(app, text ="Treeview(hierarchical)").pack()
  
# Creating treeview window
treeview = ttk.Treeview(app)  
  
# Calling pack method on the treeview
treeview.pack()  
  
# Inserting items to the treeview 
# Inserting parent
treeview.insert('', '0', 'item1', 
                text ='GeeksforGeeks'
  
# Inserting child
treeview.insert('', '1', 'item2', 
                text ='Computer Science')
treeview.insert('', '2', 'item3', 
                text ='GATE papers')
treeview.insert('', 'end', 'item4',
                text ='Programming Languages')
  
# Inserting more than one attribute of an item
treeview.insert('item2', 'end', 'Algorithm'
                text ='Algorithm')  
treeview.insert('item2', 'end', 'Data structure'
                text ='Data structure'
treeview.insert('item3', 'end', '2018 paper'
                text ='2018 paper')  
treeview.insert('item3', 'end', '2019 paper'
                text ='2019 paper')
treeview.insert('item4', 'end', 'Python'
                text ='Python')
treeview.insert('item4', 'end', 'Java'
                text ='Java')
  
# Placing each child items in parent widget
treeview.move('item2', 'item1', 'end')  
treeview.move('item3', 'item1', 'end')
treeview.move('item4', 'item1', 'end')
  
# Calling main()  
app.mainloop()

chevron_right


Output:
python-tkinter-treeview

In the above output, a hierarchical treeview is created. Where, GeeksforGeeks is the parent with Computer Science, GATE papers and Programming Languages as its child. And all the child have their respective attributes attached to them. At last, move() method is called here in order to connect all the children to the parent tree.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.