Create Treeview In Python GUI Application


In this blog, I am going to create a hierarchical treeview in Python GUI application. It will display the treeview on the application screen.
Software requirement
Python 3.5 and IDLE (Python 3.5)
Programming code
  1. #Create hiracchical treeview Application  
  2. from tkinter import *  
  3. from tkinter import ttk  
  4. app=Tk()  
  5. #App Title  
  6. app.title("Python GUI Application ")  
  7. #Lable  
  8. ttk.Label(app, text="Hierachical Treeview").pack()  
  9. #Treeview  
  10. treeview=ttk.Treeview(app)  
  11. treeview.pack()  
  12. #Treeview items  
  13. treeview.insert('','0','item1',text='Parent tree')  
  14. treeview.insert('','1','item2',text='1st Child')  
  15. treeview.insert('','end','item3',text='2nd Child')  
  16. treeview.insert('item2','end','A',text='A')  
  17. treeview.insert('item2','end','B',text='B')  
  18. treeview.insert('item2','end','C',text='C')  
  19. treeview.insert('item3','end','D',text='D')  
  20. treeview.insert('item3','end','E',text='E')  
  21. treeview.insert('item3','end','F',text='F')  
  22. treeview.move('item2','item1','end')  
  23. treeview.move('item3','item1','end')  
  24. #Calling Main()  
  25. app.mainloop()  
About the code
First, I am importing the tkinter modules.
Next, assign a class and variables and give the application title.
Next, create hierarchical treeview and add tree items in the code.
Finally, I have started the windows event loop by calling the mainloop method.
Then, let’s execute the code.