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

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

By default, the Treeview widget's rows are around 20 pixels tall so you can only fit small icons on the rows. If you use larger icons, the widget isn't smart enough to resize the rows so the icons are truncated and may overlap.

This example shows how you can change the widget's row size so you can use larger icons.

Changing Row Height

For this example, I decided to base the row height on the number of lines of text I wanted to display on each. The following code sets the necessary dimensions.

LINES_PER_ROW = 2 ICON_HGT = LINES_PER_ROW * 20 ROW_HGT = ICON_HGT * 20 // 16

This program displays 2 lines of text for each row. To make the icons a similar size, it sets their height equal to the number of lines times 20 pixels per line. The row height includes some extra space so the lines aren't too close together.

Having defined those sizes, the following snippet creates the Treeview widget.

def build_ui(self): '''Build the UI.''' style = ttk.Style() style.configure('LargeIcon.Treeview', rowheight=ROW_HGT) # Create the tree with 2 columns: #0 and title. tree = ttk.Treeview(self.window, show='tree', style='LargeIcon.Treeview') ...

This code first creates a Style object to modify the Treeview's style. It sets the Treeview widget's LargeIcon value rowheight value to the desired row height.

The snippet then passes the name of the custom style into the Treeview constructor so the widget uses the defined row height.

Loading Icons

For this program, I hard-coded the hierarchy data in Python dictionaries. The following code shows how the load_org_data function begins.

def load_org_data(): '''Load the hierarchical org data.''' return [ { "name": "Rachel Vance", "image": "Rachel Vance.png", "title": "Director of Operations", "reports": [ { "name": "Priya Nair", "image": "Priya Nair.png", "title": "Customer Success Manager", "reports": [ { "name": "Maya Lin", "image": "Maya Lin.png", "title": "Operations Coordinator", }, ...

Each person's dictionary includes an image value that gives the name of that person's picture in the local directory. In a real application, you would use a photo of the person. For this example, I used avatars that I got from pixabay.

The following snippet starts loading the data.

# Insert the data. self.icons = [] for root in self.org_data: # Insert this root and its descendants. self.insert_node(tree, root)

This snippet loops through the root nodes in self.org_data and calls the insert_node method for each to insert them into the Treeview.

Creating Nodes

The following code shows the insert_node method.

def insert_node(self, tree, node, parent_id=''): '''Recursively insert a node in this tree.''' # Get this person's image. image = Image.open(node.get('image', None)) if image is not None: image = image.resize((ICON_HGT, ICON_HGT), Image.Resampling.LANCZOS) image = ImageTk.PhotoImage(image) self.icons.append(image) # Insert the node displaying its name and title. pad = ' ' new_id = tree.insert( parent_id, 'end', text=f'{pad}{node["name"]}\n{pad}{node.get("title", "")}', open=True, image=image, ) # Add reports. for child in node.get('reports', []): self.insert_node(tree, child, new_id)

This function gets the person's image value. It uses the dictionary's get method so it can use the default value None if the dictionary does not have an image value.

If the image value is not None, the code loads the image file, resizes it to have the desired icon height, converts the result into an ImageTk.PhotoImage, and saves it in the self.icons list so it is not garbage collected and tkinter can use it later. (This example assumes all of the images are square. You can change the resizing code if that's not true, but you might have a hard time making things line up nicely if the images have different aspect ratios.)

Next, the code inserts a new node into the tree. It sets the node's text equal to the person's name and title on two lines of text. Each line begins with two spaces of padding so the text doesn't start too close to the icon. The image=image parameter makes the node display the resized image.

After it creates the new node, the function loops through this person's reports (the people who report to this person in the organization) and calls itself recursively to add their nodes.

Conclusion

Adding large icons to a Treeview requires two main changes to the previous example. First, create a Style for LargeIcon.Treeview that sets rowheight. Second, pass the Treeview constructor the style's name so the widget uses it. Then add the images as in the previous example.

Once catch is that every row in the Treeview has the same height. That means, for example, that you can't use short rows with little icons to represent departments and teams, and then use taller rows to show pictures of people. That may be just as well, though, because different row heights might make the widget look even more cluttered than it already is.

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

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