[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: Use a tkinter Treeview as a grid in Python

[This example uses a tkinter Treeview as a grid]

Some of my earlier posts explain ways you can use the Treeview widget to display hierarchical data. Tkinter doesn't include a grid widget, but you can use a Treeview as a grid. It's not perfect, but it's okay if you're not too picky.

Building the Grid

When the program starts, the following build_ui method creates a populates the Treeview.

def build_ui(self): '''Build the UI.''' self.window.rowconfigure(0, weight=1) self.window.columnconfigure(0, weight=1) # Create the Treeview. columns = ('title', 'year', 'pages', 'isbn') tree = ttk.Treeview(self.window, columns=columns, show='headings') self.tree = tree tree.focus_force() # Add vertical scrollbar. scrollbar = ttk.Scrollbar(self.window, orient=tk.VERTICAL, command=tree.yview) tree.configure(yscrollcommand=scrollbar.set) # Perform layout. tree.grid(row=0, column=0, sticky=tk.NSEW) scrollbar.grid(row=0, column=1, sticky=tk.NS) # Give alternate rows different colors. tree.tag_configure('odd', background='#ffffff') tree.tag_configure('even', background='#f0f4f8') # Set column headings. tree.heading('title', text='Title') tree.heading('year', text='Year') tree.heading('pages', text='Pages') tree.heading('isbn', text='ISBN') # Set column widths and alignments. tree.column('title', width=300, anchor=tk.W) tree.column('year', width=50, anchor=tk.E) tree.column('pages', width=50, anchor=tk.E) tree.column('isbn', width=100, anchor=tk.W) # Make some data: Title, Year, Pages, ISBN. book_data = [ ['Build Your Own Python Action Arcade!', 2025, 292, '979-8272041388'], ['Build Your Own Ray Tracer With Python', 2024, 356, '979-8333505439'], ['Beginning Database Design Solutions', 2022, 720, '978-1119901709'], ['Beginning Software Engineering', 2022, 682, '978-1119901716'], ['Essential Algorithms', 2019, 769, '978-1119575986'], ['WPF 3d: Three-Dimensional Graphics with WPF and C#', 2018, 426, '978-1983905964'], ['Interview Puzzles Dissected', 2016, 300, '978-1539504887'], ] # Insert the data. for i, row in enumerate(book_data): tag = 'even' if i % 2 == 0 else 'odd' tree.insert(parent='', index='end', values=row, tags=(tag,))

This example adds a vertical Scrollbar in case the Treeview is too tall to fit on the window. One nice feature of the Treeview is that it only scrolls its data area so the column titles are still visible even if you scroll down.

To make the Scrollbar and Treeview resize properly, the code first sets the first row and column weights to 1 so they expand to fill the available area.

Next, the code creates the Treeview widget. In the call to the widget's constructor, the program sets the show parameter to "headings" to make the widget display the column headings and not display a tree structure. (This means the widget hides column #0, which displays the expand/contract icons.)

The code then creates the vertical Scrollbar and attaches it to the Treeview.

The Treeview doesn't display borders between rows or columns. To make it easier to read the rows, particularly for very wide grids, many programs give alternate rows different background colors. To do that, the program uses tag_configure to define two tags named odd and even that select different background colors.

The method then sets the columns' headings and widths as in previous posts. It also sets the columns' alignments, which wasn't part of the earlier posts.

Next, the code defines some data in the form of a list of lists. (This data is about some of my previous books. Please look them up online and support me by buying any that you find interesting.)

The code then enumerates the data. If the current row's number is even, the code sets tag to 'even'. If the row number is odd, it sets tag to 'odd'. The code then inserts the row's data into the Treeview, giving it the assigned tag value. The widget uses the tag to set the row's background color.

Conclusion

The Treeview can display grid-like data, but it doesn't provide many of the features that true grid controls do. It lets you resize the columns, but it doesn't let you edit values, sort the data, decide which columns should be visible, draw row or column lines, or display images. Some of my previous posts show how to display images in a Treeview, but the widget can only display images in column #0 and that column is hidden when you use the widget as a grid.

Still, if you just want to display some data relatively easily, the Treeview can do the job. If you want to do anything more, you'll probably need to bild your own gid by using Label, Entry, and other widgets.

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

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