Treeview in Tk (tkinter)
The tree view (or hierarchical list) is a UI widget that presents data in hierarchical form, although non-hierarchical data is also supported. It can be used as a table widget too. It was introduced in Tk 8.5 and in Python it is provided by the tkinter.ttk
standard module.
(See the source code of this simple file explorer below.)
To include a tree view widget in your Tk application, create a an instance of the ttk.Treeview
class.
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
root = tk.Tk()
|
|
root.title("Treeview in Tk")
|
|
treeview = ttk.Treeview()
|
|
treeview.pack()
|
|
root.mainloop()
|
Adding and Retrieving Items¶
Once the treeview
object is created, add rows or items via the insert()
method:
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
root = tk.Tk()
|
|
root.title("Treeview in Tk")
|
|
treeview = ttk.Treeview()
|
|
treeview.insert("", tk.END, text="Item 1")
|
|
treeview.pack()
|
|
root.mainloop()
|
Since the tree view presents hierarchical data, the first argument passed to insert()
is either a parent item or an empty string (""
), meaning that the new item has no predecessor.
The second argument is the position (index) which you want to insert the item in, or tk.END
to place it at the end of the tree. So insert("", 0, text="Item 1")
places the item at the beginning.
The insert()
method returns the ID of the created item, usually abbreviated as iid
. To create a child item within Item 1, use:
Even Subitem 1 might have other items within it, and so on.
Tk automatically creates an ID for each item, which is just a string in the form I001
, I002
, etc. You will rarely want to create an ID for a new item yourself; but if necessary, you can do it by specifying the iid
parameter.
This might be useful to get or modify the item's data later. For example, the following code displays the text of the item in a message box when the user presses a button:
The item()
methods lets you both read and modify item's data. The previous code displays in message box the text
property of the item with ID my_iid
. If no option is specified, the function returns a dictionary with the item's data.
The info
dictionary will be something like:
{'text': 'Item 1', 'image': '', 'values': '', 'open': 0, 'tags': ''}
To change any of these values, simply pass it as an argument:
# Change the text of an item. treeview.item(my_iid, text="New text")
Items within the tree or contained in another item can be obtained via the get_children()
method, which returns a tuple of IDs.
treeview_children
is a tuple containing the IDs (strings) of every root item (i.e., does not includes subitems) within the tree. The item_children
tuple contains every subitem within Item 1 (whose ID is stored in item
).
Moving and Deleting Items¶
It is possible to move an item from one place to another using move()
. For example, if you have inserted these items:
item1 = treeview.insert("", tk.END, text="Item 1") item2 = treeview.insert("", tk.END, text="Item 2")
You can move Item 1 into Item 2 this way:
treeview.move(item1, item2, tk.END)
The move()
method receives the item to be moved as the first argument. The second argument must be either another item into which you want to place the original item, or ""
in order to place it at the root of the tree view. The third argument is the position or row index where the new item is moved.
Tk provides two functions to remove one or more items: delete()
and detach()
. The difference is that the former removes the passed items both from the widget and from memory. The later just unlinks them from the tree, and they might be re-linked later using move()
.
# Removes item 2. treeview.delete(item2) # Unlinks item 1 (might be re-linked later). treeview.detach(item1)
After these calls, the exists()
method, which lets you know whether an items exists within the tree, returns False
for exists(item2)
but True
for exists(item1)
.
print(treeview.exists(item2)) # False. print(treeview.exists(item1)) # True.
To remove all items from the tree view use:
treeview.delete(*treeview.get_children())
Miscellaneous Functions¶
The focus()
returns the ID of the item that has focus (which may differ from the selected item) or an empty string if there is no focus. To put the focus on a certain item, pass the item ID:
treeview.focus(item) # Set the focus on item. print(treeview.focus()) # Retrieve the id of the item that has focus.
The index()
method returns the position of the specified item. Note that this position is relative to the parent item. For this reason, two subitems might have the same position if they have different parents.
item1 = treeview.insert("", tk.END, text="Item 1") item2 = treeview.insert("", tk.END, text="Item 2") print(treeview.index(item1)) # 0 print(treeview.index(item2)) # 1
Creating Columns¶
A tree view can have multiple columns. Thus it is often used as a table widget. For example, if you need to display a hierarchical list of files and folders, the text of each item could be used to show the file name while adding two extra columns to display its size and last modification.
You will first need to configure the Treeview
instance with the required columns:
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
|
|
root = tk.Tk()
|
|
root.title("Treeview in Tk")
|
|
treeview = ttk.Treeview(columns=("size", "lastmod"))
|
|
treeview.pack()
|
|
root.mainloop()
|
Then you will be able to add a new row to represent a file like this:
Thus, you specify that the item text is "README.txt"
via the text
parameter (which you might think of as the first column), while the values ("850 bytes"
and "18:30"
) of the extra columns (size
and lastmod
) are passed via the values
parameter.
It is also possible to customize the title of each column.
Internally, Tk uses the name #0
to refer to the first column that is created by default and where the item's text is displayed, while size
and lastmod
are arbitrary names that you choose during the creation of treeview
to refer to the extra columns.
To get the value of the columns of a given item, call the set()
method (yes, the name is a little confusing).
item = treeview.insert( "", tk.END, text="README.txt", values=("850 bytes", "18:30") ) # Prints {'lastmod': '18:30', 'size': '850 bytes'} print(treeview.set(item))
To query the value of a specific column, add a second argument with the column identifier.
# Prints 18:30 print(treeview.set(item, "lastmod"))
And to change its value, pass a new value as the third argument:
# Change 18:30 to 19:30. treeview.set(item, "lastmod", "19:30")
Icons¶
Tk lets you include an icon image for an item via the image
parameter.
The file.png
file (which you can download from the ZIP file at the bottom) must be located in the current working directory. If it is stored elsewhere in the file system, pass its full path instead.
Note that due to a Tcl/Tk limitation, a reference to the image (icon
) must be maintained. The following code runs fine, but the image is not displayed because the PhotoImage
object gets garbage-collected:
treeview.insert( "", tk.END, text="README.txt", values=("850 bytes", "18:30"), # Error: a reference to the image is not kept in memory. image=tk.PhotoImage(file="file.png") )
You might pass the content of an image instead of a file path to tk.PhotoImage
. This is useful when you need to load an image from memory:
# Load image from memory. # image_content must be a string or a bytes instance. icon = tk.PhotoImage(data=image_content)
Managing Selection¶
The selection()
method returns a tuple containing the IDs of the selected items, or an empty tuple if there is no selection. For example, the following code set ups a button that displays a message box with the text of the selected item:
By default, a tree view allows multiple items to be selected. To allow only one selection, the tk.BROWSE
value must be passed to the selectmode
parameter.
treeview = ttk.Treeview(selectmode=tk.BROWSE)
Other functions for handling selected items include:
Method |
Description |
---|---|
|
Adds items to the selection. |
|
Removes items from the selection. |
|
Similar to |
|
Toggles the selection of an item. |
For example, the following code selects Items 1 and 2 after adding them to the table:
item1 = treeview.insert("", tk.END, text="Item 1") item2 = treeview.insert("", tk.END, text="Item 2") treeview.selection_add(item1) treeview.selection_add(item2) # Or simply: # treeview.selection_add((item1, item2))
Events¶
You might be notified when an item gets selected and, if it contains subitems, when it gets opened or closed, by binding these virtual events: <<TreeviewSelect>>
, <<TreeviewOpen>>
and <<TreeviewClose>>
.
In Tk, events need to be associated with a tag (mytag
in the previous code), and then an item can be associated with one or more tags to respond to those events.
Simple File Explorer¶
Simple file and folder browser using ttk.Treeview
.
Download: explorer.zip.