t
[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Load hierarchical data and display it a Treeview widget in Python

[This program loads hierarchical data from an indented string and displays it in a Treeview widget]

My post Parse hierarchical data from a string in Python explained one way to parse hierarchical data. My post Display hierarchical data in tkinter's Treeview widget in Python shows how to display hierarchical data in a Treeview widget.

This post combines those two techniques.

Building the UI

When the program starts, it uses the following code to load its data and build its user interface.

# Load the org chart data. self.org_data = load_org_data() # Build the UI. self.build_ui()

This snippet first calls the load_org_data function described in my post Parse hierarchical data from a string in Python explained one way to parse hierarchical data. See that post for details.

It then calls the following build_ui method to create the user interface and display the data.

def build_ui(self): '''Build the UI.''' # Create the tree with 2 columns: #0 and title. tree = ttk.Treeview(self.window, show='tree headings', columns=('title',)) # tree = ttk.Treeview(self.window, show='tree', # columns=('title',)) self.org_chart = tree self.org_chart.pack(side=tk.TOP, expand=True, fill=tk.BOTH, padx=5, pady=5) # Set column headings. tree.heading('#0', text='Name') tree.heading('title', text='Title') # Set column widths. tree.column('#0', width=200) tree.column('title', width=200) # Insert the data. for root in self.org_data: # Insert this root and its descendants. self.insert_node(tree, root)

This code creates a Treeview widget, sets its column headings, and sets the columns' widths. Notice that the call to the Treeview constructor uses show='tree headings' so the widget will display a tree with column headings.

Next, the program loops through all of the top-level root nodes saved in self.org_data. For each top-level node, it calls the insert_node method passing it the Treeview widget and the root node.

Inserting Nodes

The insert_node method is where the real magic happens. It takes as parameters the Treeview widget and an object to add to the tree. Recall from my earlier post that the object is a dictionary with name value equal to the object's name and children value set to a list holding the object's children in the tree hierarchy.

The method also takes a parent_id parameter to gives the ID of the node's parent in the tree. That parameter defaults to an empty string, which is the value you use to make the node a top-level root node.

The following code shows how the insert_node method works.

def insert_node(self, tree, node, parent_id=''): '''Recursively insert a node in this tree.''' # Insert the node. Use its name for #0 and extra fields for values. new_id = tree.insert( parent_id, 'end', text=node['name'], values=(node.get('title', ''),), open=True, ) for child in node.get('children', []): self.insert_node(tree, child, new_id)

This code first calls the Treeview widget's insert method to insert the current node under its parent. If this is the first call to insert_node, parent_id gas defaulted to an empty string so the node is a top-level root in the tree. The code saves the new node's ID in new_id.

Next, the code loops through the node's children list and recursively calls insert_node to add the children to the tree. It passes the new node's ID to the recursive call so the children are added below the new node.

Conclusion

I'll post a few more Treeview examples in the next few days. Meanwhile, download the example to experiment with it and to see additional details.
© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.