[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: Display icons on tkinter's Treeview widget in Python

[This example displays icons on tkinter's Treeview widget]

Adding icons to the nodes displayed by a Treeview widget is pretty easy. In fact, the most time-consuming part of building this example was finding nice icons to use. After reviewing a bunch of possible sources, I ended up using the icons at Bootstrap.

Loading Icons

To add an icon to a node, simply pass an ImageTk.PhotoImage object into the Treeview widget's insert method. As always, be sure to save a non-volatile copy of the image so the garbage collector doesn't throw it away and tkinter can use it when necessary.

In this example, I wanted different levels of the hierarchy to display different icons.

Company
Division
Department
Team
Person

The following code shows how the program loads its data and icons.

# Load the org chart data. self.org_data = load_org_data() # Load icons for different levels in the tree. icon_files = [ 'buildings.png', # Company 'building.png', # Division 'diagram-3.png', # Department 'person.png', # Team 'person-arms-up.png', # Person ] # Rows are 20 pixels so use 16-pixel icons. self.icons= [] for file in icon_files: image = Image.open(file) image = image.resize((16, 16), Image.Resampling.LANCZOS) self.icons.append(ImageTk.PhotoImage(image)) # Build the UI. self.build_ui()

The code first calls load_org_data to load the program's hierarchical data as in previous examples.

Next, the code creates a list of icon file names. It then loops through the list, opens each image file, resizes the image to 16×16 pixels, converts the PIL image into an ImageTk.PhotoImage, and saves the result in the self.icons so a non-volatile copy is available whenever tkinter needs it. For example, nodes at level 1 of the tree should use the icon self.icons[1].

Normally the Treeview makes its rows around 20 pixels tall so 16×16 is the largest image you can safely use without the image being truncated.

The code then calls the build_ui method to build and populate the Treeview widget.

Displaying Icons

As in previous examples, the build_ui method creates the Treeview and loops through the hierarchical data calling the insert_node method to add the nodes to the tree. The only changes are in the insert_node method, which is shown in the following code with the changes highlighted in blue.

def insert_node(self, tree, node, depth=0, 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, image=self.icons[depth], ) for child in node.get('children', []): self.insert_node(tree, child, depth + 1, new_id)

The new depth parameter gives the new node's depth in the tree and that determines which icon it should use. When it calls the Treeview's insert method, the code uses the appropriate icon for the image parameter.

The last change is in the recursive call to insert_node. In that call, the code increments depth so the new node's children use the next icon in the self.icons list.

Conclusion

Download the example to experiment with it and to see additional details.

© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.