Populating nested items in a Treeview
While ttk.Treeview
can be used as a regular table, it may also contain hierarchical structures. This is shown as a tree where items can be expanded to see more nodes of the hierarchy.
This is useful to display the results of recursive calls and several levels of nested items. In this recipe, we will take a look at a common scenario that fits with this kind of structure.
Getting ready
To illustrate how to recursively add items in a ttk.Treeview
widget, we will create a basic filesystem browser. Expandable nodes will represent folders, and once opened, they will show the files and folders that they contain:

How to do it...
The tree will be initially populated by the populate_node()
method, which lists the entries in the current directory. If an entry is a directory, it also adds an empty child to show it as an expandable node.
When a node that represents a directory is opened, it lazily loads the contents of the directory by calling populate_node()
again. This...