Updated June 20, 2023
Definition of Tkinter TreeView
Tkinter Treeview is a module in Python that developers use to create visually appealing GUI (Graphical User Interface) elements. With Tkinter Treeview, developers can utilize a tk-based toolkit to design and customize various elements of the user interface. Tkinter Treeview consists of the GUI toolkit tk, which provides an object-oriented approach to the programmers for using it as part of the development. It consists of many other controls and widgets like text boxes, buttons, scroll bars, etc., incorporated with some management methods to organize these widgets.
Syntax:
w = Entry( _master, _options, ... )
- _master: This parameter represents the parent window in Tkinter Treeview module.
- _options: The module consists of a range of common methods that are immediately utilized when an option is selected from the available list.
List of Methods
- get()
- icursor(index)
- index(index)
- insert(index, s)
- select_clear()
- select_adjust(index)
- select_present()
- select_from(index)
- select_range(start, end)
- xview_scroll(number, what)
- xview(index)
- delete(first, last=None)
List of Options
- bd
- bg
- cursor
- command
- exportselection
- font
- highlightcolor
- fg
- justify
- selectbackground
- relief
- selectforeground
- selectborderwidth
- textvariable
How to Create TreeView in Tkinter?
The module present in Tkinter offers a wide range of options and methods for creating a Treeview, which is extensively used to design visually appealing and feature-rich GUIs (Graphical User Interfaces). This module leverages the tk GUI toolkit as a package, providing Python web developers with many tools and features to create attractive user interfaces. Let’s check how to create a Treeview using Tkinter for developing any specific kind of application :
- As mentioned, Tkinter module plays a pivotal role; thus, it is very much needed to import the Tkinter module for initiating further applications related to the application.
- Creating the main window as represented and mentioned in the syntax, which acts like a container, means this main window will reside inside this container.
- Since the main window acts as a parent window, it is possible to add any number of widgets.
- Further, this widget will comprise of options list which needs to be triggered and applied to the widget to provide any widget options to perform required operations.
- Still, there are two most important methods, which are the kind of mandate to perform any action related to Python for creating a web GUI(Graphical User Interface).
- The two methods include:
#Creation of tk represented as
Tk(screenName=None,baseName=None, className=’Tk’, useTk=1), which is used to create a window including all these parameters followed by the main loop.
How the method gets created is the main question, and it is used by calling any class using the name of the parent main window like :
o = tkinter.Tk() where o represents the name of the main window.
#mainloop()
This method is used when the application is ready to run and is an infinite loop used for running the application, followed by waiting for an application and then processing the entire event till the time it is not closed.
Tkinter also has one more interesting feature where the toolkit provides access to the number of geometric configurations of the widgets that are used for varied reasons of arranging and organizing the widgets in the entire container of the parent window. There are mainly three geometry manager class comprising methods related to geometric orientation, which are as follows:
- Pack method()
- Place method()
- Grid method()
Tkinter treeview, with all its features, is quite promising for making the entire view desirable as part of web development.
Examples of Tkinter TreeView
Lets us discuss examples of Tkinter TreeView.
Example #1
This program demonstrates the usage of the Tkinter module to import and utilize options within a window.
Code:
from tkinter import *
ms = Tk()
vr_1 = IntVar()
Checkbutton(ms, text='Veg', variable=vr_1).grid(row=0, sticky=W)
vr_2 = IntVar()
Checkbutton(ms, text='Non-Veg', variable=vr_2).grid(row=1, sticky=W)
mainloop()
Output:
Example #2
This program demonstrates the use of a widget where the user uses entry text for creating Student details. The user uses to enter text in a single line and then uses multi-line text input used for the creation of the widget.
Code:
from tkinter import *
ms = Tk()
Label(ms, text='Student_Name').grid(row=0)
Label(ms, text='Student_Id').grid(row=1)
Label(ms, text='Student_stream').grid(row=2)
Label(ms, text='Student_marks').grid(row=3)
o1 = Entry(ms)
o2 = Entry(ms)
o3 = Entry(ms)
o4 = Entry(ms)
o1.grid(row=0, column=1)
o2.grid(row=1, column=1)
o3.grid(row=2, column=1)
o4.grid(row=3, column=1)
mainloop()
Output:
Example #3
This program demonstrates the Tkinter hierarchical treeview through a university and various options to select accordingly from the list as shown in the output.
Code:
from tkinter import *
from tkinter import ttk
appln = Tk()
appln.title("Application for GUI to represent the University ")
ttk.Label(appln, text ="Treeview(hierarchical)").pack()
treeview = ttk.Treeview(appln)
treeview.pack()
treeview.insert('', '0', 'i1', text ='University')
treeview.insert('', '1', 'i2', text ='Computer Science')
treeview.insert('', '2', 'i3', text ='Mechanical_Engg')
treeview.insert('', 'end', 'i4', text ='Civil_Engg')
treeview.insert('i2', 'end', 'Analysis_Of_DS', text ='Analysis_Of_DS')
treeview.insert('i2', 'end', 'OOPS', text ='OOPS')
treeview.insert('i3', 'end', 'Mech_Exam', text ='Mech_Exam')
treeview.insert('i3', 'end', 'CAD', text ='CAD')
treeview.insert('i4', 'end', 'Civil_Exam', text ='Civil_Exam')
treeview.insert('i4', 'end', 'Drawing', text ='Drawing')
treeview.move('i2', 'i1', 'end')
treeview.move('i3', 'i1', 'end')
treeview.move('i4', 'i1', 'end')
appln.mainloop()
Output:
Conclusion
Tkinter Treeview is a very useful standard library in Python that is used for creating a very enhanced GUI [Graphical User Interface] with features that aid programmers in manipulating and performing UI-related actions seamlessly. It helps to provide end-users with a user-friendly, flexible, and versatile user interface to work and cooperate.
Recommended Articles
This is a guide to Tkinter TreeView. Here we discuss the definition of Tkinter TreeView, how to work Tkinter TreeView, and sample code for better understanding. You may also have a look at the following articles to learn more –