Tree view widgets and Tree view scrollbar in Tkinter-Python

GUI stands for Graphical User Interface and it is the graphical design on the interface like icons, menus using which a user can interact with an application. Tkinter is a Python module designed to develop the GUI of any project, from an app to a website. It is one of the most widely used modules to create GUI faster. The functionality of the module makes the work of the designer easier.

To use any function in the module, we need to import Tkinter:

Using Tkinter, we can display information in hierarchical representation using Treeview. This tutorial explains the following contents:

  1. Tree view
  2. Tree view widgets
  3. Tree view scrollbar

Let us now break the words down; we'll learn about each of them one after the other:

  1. A Tree view, also known as an "Outline view," is the graphical visualization of a hierarchical representation of data with every item having more sub-items.
  2. A Tree view widget is the interaction of the user to browse with the represented hierarchical representation. There can be two types of Tree view widgets in Tkinter:
    1. Tree table widget: Displaying the structures data in tabular format in rows and columns where rows represent items/ entities and columns represent attributes of the items.
    2. Hierarchical widget: It is the representation of the data level-wise. There can be any number of top-level nodes, and only if we click on the extension will the sub-nodes appear. A top node can have second-level and third-level nodes also.
      Here is an example of Tree view hierarchical representation of data:
      Tree view widgets and Tree view scrollbar in Tkinter-Python
  3. A Tree view scrollbar is a regular scrollbar using Tree view widgets.

First, let us cover the foundation of Tree view widgets:

For creating a Tree view widget, we need to import the tkinter.ttk module. Tkinter has two types of widgets-classic and themed. To use themed widgets, we need to use the ttk class of the tkinter module.

Tabular representation:

Output:

Tree view widgets and Tree view scrollbar in Tkinter-Python

First, we imported the Tkinter module, the ttk sub-module, and the needed features(show info). We created a root window called root_window, gave it a name, and gave dimensions to it.

Using ttk.Treeview(), we created a Table Tree view widget with the root_window and columns, and by specifying 'headings' to show, we are telling the function to show the header row in the table. Now, as we create a tabular representation, we specify the names of the columns in a tuple.

Finally, we assigned headings to the columns, inserted data into them, and created a function to handle the event (When clicked on a row in the table, it displays a message). By configuring it, we set the table in the window.

Hierarchical Representation:

Here is a program with an example of the Hierarchical representation we discussed above with courses in engineering:

Output:

Tree view widgets and Tree view scrollbar in Tkinter-Python

We followed the same steps from tabular representation till creating a Tree view widget. After creating a widget, we insert elements into the widget with an iid, and this iid identifies every item individually. After inserting all the items, we use the move() function to specify the sub-items by making changes to the indentation

You can observe that only if we click on the hierarchy all the sub contents will get displayed.

Treeview Scrollbar:

A regular scrollbar can use Treeview widgets, which is called the "Treeview scrollbar". Now that we learned about the Treeview widgets, let us try to create a Treeview scrollbar:

Output:

Tree view widgets and Tree view scrollbar in Tkinter-Python Tree view widgets and Tree view scrollbar in Tkinter-Python




Latest Courses