Python

Tkinter Treeview

A well-liked Python library called “Tkinter” is used to build graphical user interfaces (GUIs), and one of its most potent and adaptable widgets is called “tkinter-treeview“. This article explores the characteristics and applications of “tkinter-treeview” to provide readers with a thorough understanding of the program.

What is Tkinter Treeview?

“Tkinter-treeview” is a specialized widget in “Tkinter” that presents hierarchical data structures in a tree-like format. It supports the display of multiple columns, headers, and a variety of interaction options, making it suitable for representing complex data sets with ease. The “tkinter.ttk” package is where the Python Tkinter Treeview comes from.

How to Utilize Tkinter Treeview?

To utilize “tkinter treeview”, developers must import the necessary module and instantiate the widget. This involves creating a parent container and configuring the desired columns, headers, and data. Developers can then populate the widget with items and efficiently manage the behavior through numerous options and event bindings.

Syntax

tree = ttk.Treeview(container, **options)

 

In this syntax, the options can be:

columns: Represents the list of column names.

displaycolumns: The string “#all” or a list of column identifiers (either symbolic or numeric indices) describing which data columns are displayed and in what order.

height: Refers to several visible rows.

padding: It specifies the widget’s internal padding either an integer or a list of four items if possible.

selectmode: “Extended,” “browse,” or “none” are the options. If “extended” is selected (the default), additional things can be chosen. When utilizing the “browse” option, a single item can only be selected at a time. If “none,” the user cannot alter their choice.

show: It refers to a list indicating which tree elements should be displayed, with zero or more of the following values.

Example 1: Create a Basic Treeview

To create a basic “treeview”, consider the following code:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
tree = ttk.Treeview(root)
tree.pack()
root.mainloop()

 

In the above code, instantiate a “Treeview” widget and create a basic “Tkinter” window. The “pack()” method is then used to fit the “Treeview” widget into the window. To display the window, finally, launch the Tkinter event loop using “root.mainloop()”.

Output

Customization Options

“Tkinter-treeview” provides extensive options for customizing the appearance and behavior of the widget. These options include adding text, setting column widths, specifying font styles, and selecting desired highlight colors. Furthermore, developers can manipulate features such as drag-and-drop functionality, checkboxes, and editable cells to enhance interactivity.

Example 2: Create a Customizable Treeview

Look at the code below that creates a customizable Treeview:

from tkinter import *
from tkinter import ttk
 
tree = Tk()
tree.title('Python')
tree.geometry('400x300')
tree['bg']='blue'
 
t = ttk.Treeview(tree)
t['columns']=('Id', 'Name', 'Email')
t.column('#0', width=0, stretch=NO)
t.column('Id', anchor=CENTER, width=80)
t.column('Name', anchor=CENTER, width=80)
t.column('Email', anchor=CENTER, width=120)
 
t.heading('#0', text='', anchor=CENTER)
t.heading('Id', text='Id', anchor=CENTER)
t.heading('Name', text='Name', anchor=CENTER)
t.heading('Email', text='Email', anchor=CENTER)
 
t.insert(parent='', index=0, iid=0, text='', values=('1','George','[email protected]'))
t.insert(parent='', index=1, iid=1, text='', values=('2','Meredith','[email protected]'))
t.insert(parent='', index=2, iid=2, text='', values=('3','Isobel','[email protected]'))
t.insert(parent='', index=3, iid=3, text='', values=('4','Derek','[email protected]'))
 
t.pack()
tree.mainloop()

 

In this code snippet:

  • The “Python” GUI window is “400×300 pixels” in size and the window’s background is currently set to “blue”.
  • Three columns “Id”, “Name”, and “Email” are set up in the Treeview widget. The columns are aligned in the center and have defined widths. Also, each column’s headers are specified.
  • The “Treeview” receives four rows of data, each containing an “Id”, “Name”, and “Email” value. The main event loop is then initiated to display the GUI, and the Treeview widget is finally placed inside the main window.

Output

Conclusion

Understanding and utilizing the “tkinter-treeview” widget in Tkinter is invaluable for developers seeking to develop sophisticated GUI applications. Its extensive features, customization options, and data manipulation capabilities make it a powerful tool for representing and interacting with complex hierarchical data.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.