Title: Display hierarchical data in tkinter's Treeview widget in Python
My previous post Parse hierarchical data from a string in Python explained one way to parse hierarchical data. My next post will show how to display that data in a Treeview widget. Before we get there, though, this post shows how you can use the widget's methods to "manually" add items to a hierarchy.
Preparing the Treeview
The example program's build_ui method creates and populates the Treeview widget. The following code shows how the method begins.
def build_ui(self):
'''Build the UI.'''
# Create the tree with 2 columns: #0 and title.
tree = ttk.Treeview(self.window, show='tree',
columns=('title',))
tree.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)
...
This code creates the Treeview widget and packs it so it fills the window.
Next, it defines text names for the widget's columns. First it sets the text for column #0 to Name. Note that column #0 is automatically created by the widget and it holds things like the expand/collapse boxes, indentation, item icons, and other stuff. In a later post, I'll show how to hide this column, but for hierarchical data it's useful.
The code then defines a column named title with text Title. You can create more columns if that's useful.
If you look at the picture of the example program, you won't see the column text. The reason is the program created the widget with the parameter show='tree'. That tells the widget to display only the tree and not the column text. If you change that parameter to show='tree headings', you'll see the tree and its column text as in the picture on the right.
After defining the columns and their text, the code sets the columns' widths. Notice how it uses the column names to tell with widget which column it wants to modify.
The following code shows the rest of the build_ui method.
# Insert some nodes "manually."
company_id = tree.insert(parent='', index='end',
text='Snort & Giggle, LLC.', open=True)
anita_id = tree.insert(parent=company_id, index='end',
text='Anita Nap',
values=('Chief Executive Officer (CEO)',),
open=True)
justin_id = tree.insert(parent=anita_id, index='end',
text='Justin Case',
values=('VP of Risk & Overthinking',),
open=True)
may_id = tree.insert(parent=justin_id, index='end',
text='May B. Later',
values=('Director of Procrastination',),
open=True)
tree.insert(parent=may_id, index='end',
text='Al Dente',
values=('Senior Deadline Stretcher',),
open=True)
tree.insert(parent=may_id, index='end',
text='Paige Turner',
values=('Intern, Unread Memos',),
open=True)
tree.insert(parent=justin_id, index='end',
text='Brock Lee',
values=('Lead Burnout Prevention Specialist',),
open=True)
bea_id = tree.insert(parent=anita_id, index='end',
text='Bea Minor',
values=('VP of Low-Key Operations',),
open=True)
stan_id = tree.insert(parent=bea_id, index='end',
text='Stan Dupp',
values=('Director of Endless Meetings',),
open=True)
tree.insert(parent=stan_id, index='end',
text='Carrie Oakey',
values=('Morale & Karaoke Coordinator',),
open=True)
tree.insert(parent=stan_id, index='end',
text='Tim Burr',
values=('Specialist, Falling Asleep on Zoom',),
open=True)
tree.insert(parent=bea_id, index='end',
text='Ella Vator',
values=('Manager of Escalations',),
open=True)
robin_id = tree.insert(parent=anita_id, index='end',
text='Robin Banks',
values=('Chief Financial Officer (CFO)',),
open=True)
tree.insert(parent=robin_id, index='end',
text='Bill Ding',
values=('Director of Overhead Costs',),
open=True)
tree.insert(parent=robin_id, index='end',
text='Penny Less',
values=('Junior Budget Analyst',),
open=True)
To add a new node to the hierarchy, you call the widget's insert method passing it:
- parent
- The ID of the parent node that should contain the new node. Use an empty string '' to make the new node a top-level node with no parent.
- index
- The position the new node should have in relation to siblings. This can be a position like 0 or 1, or 'end' to put the new node at the end of the siblings.
- text
- The text that the new node should display.
- values
- A list of values that should go in the node's columns. If you use a tuple holding a single value, be sure to end with a comma so Python knows it's a tuple and not a parenthesized expression.
- open
- True if you want the new node to be expanded (if it's a parent node).
The build_ui method calls insert to create the top-level root node and saves the returned ID in company_id. It then calls insert again, passing it the ID company_id to add the Anita Nap node below the root node. It saves the returned ID so it can add nodes below that one.
The code continues building the tree one node at a time. It's tedious but straightforward.
Conclusion
That's all you need to do to display a hierarchy. Of course, hard-coding the tree structure into the program isn't very flexible. In my next post, I'll explain how you can display the hierarchical data parsed by the previous example.
Until then, download the example to experiment with it and to see additional details.
|