Definition of Tkinter TreeView
Tkinter Treeview is a module in python which is used for constructing some attractive GUI (Graphical User Interface) for the python developers. Tkinter Treeview is one of the standard libraries in python exclusively used for designing the user interfaces and provides web developers to play around with this tk based toolkit. 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: List of many common methods are present in the module which is used as soon as the options get selected with one of the options available in the 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?
There are many options in a list and method responsible for creating a Treeview in Tkinter and is profusely used for creating attractive and enhances GUI(Graphical User Interface). The module present is responsible for creating User interfaces using a varied list of options and methods present in the tk GUI toolkit as a package which when imported provides a lot of aid to python web developers. 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.
- Create 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 for providing 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 for creation of window including all these parameters followed by mainloop.
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 of an application and then processes 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 of methods related to geometric orientation which are as follows:
- Pack method()
- Place method()
- Grid method()
There are many options list used for the creation of widgets and then used for organizing widgets with its orientation to enhance the view and visibility of the Tkinter Treeview. Thus Tkinter treeview with all its features are 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 is used to demonstrate the Tkinter module to import and make use of options within the window like in the given program, use of Checkbutton is made as part of the options list to select the option between veg and Non-Veg as shown in the output.
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 makes use of entry text for creating Student details that are used by the user to enter text in a single line and then used for multi-line text input used for creation of the widget. With this lot, many options can be created easily as shown in the output.
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 is used to demonstrate 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 which is used for creating a very enhanced GUI [Graphical User Interface] with features that aid programmers to manipulate and perform UI related actions seamlessly. It helps to provide end-users 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 along with sample code for better understanding. You may also have a look at the following articles to learn more –