[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: Let the user click links in a tkinter Treeview in Python

[When you click on a row's URL field, the program opens the URL in your browser]

My post Use a tkinter Treeview as a grid in Python shows how you can use a tkinter Treeview to display row and column data in a grid. This example explains how you can let the user click a URL field to open that URL in the default browser.

I won't include the code that adds the URL column to the grid. See my previous post for details about adding data to the Treeview.

Styling Rows

The previous post gave odd and even numbered rows different background colors so it's easy to see the rows. This post adds a blue foreground color to make the data look more link-like.

# Give alternate rows different colors. tree.tag_configure('odd', background='#ffffff', foreground='#0000FF') tree.tag_configure('even', background='#f0f4f8', foreground='#0000FF')

Unfortunately, the Treeview cannot give different cells within a row different appearances. In this example, it would be nice if the URL column was blue and underlined while the other cells were black without underline. Sadly the Treeview gives every cell in a row the same appearance so that's not an option.

One alternative would be to make every cell blue and underlined and open the URL when the user clicks on any cell. That may be the best approach, but I decided to use this compromise so I could show how to change the cursor and take action when the mouse is on a particular cell.

Finding Cells

When you move or click the mouse over the Treeview widget, the program needs to figure out what cell is under the mouse. The following get_cell_id helper function returns the logical name of the cell under the mouse, if there is one.

def get_cell_id(tree, x, y): '''Return the logical name of the cell at this spot.''' region = tree.identify_region(x, y) if region != 'cell': return None column_id = tree.identify_column(x) return tree.column(column_id, 'id')

This function calls the Treeview's identify_region method to see what part of the Treeview is under the mouse. That method can return:

  • heading
  • separator
  • tree
  • cell
  • nothing

If the mouse is not over a cell, the get_cell_id function returns None to indicate that the mouse is not over a cell.

If identify_region returns cell, the function calls identify_column to see what column is under the mouse. That method returns the column identifier as in #0, #1, and so forth.

If you like, you can use that value directly to see if you have the right column. For example, if column_id == '#5' ...

It's probably better, though, to convert the column ID into something more comprehensible like the column's logical name url or its label URL. The get_cell_id function calls the Treeview's column method to get the column's logical ID and returns it.

Tracking Mouse Position

When the program creates its Treeview widget, it calls the following code to track mouse events.

# Watch for mouse move and mouse up. tree.bind('', self.mouse_move) tree.bind('', self.mouse_up)

When the mouse moves over the Treeview, the following mouse_move event handler executes.

def mouse_move(self, event): # Display the pointer cursor if we're over the link field. if get_cell_id(self.tree, event.x, event.y) == 'url': self.tree.config(cursor='hand2') else: self.tree.config(cursor='')

This method calls the get_cell_id helper function to see which cell is under the mouse. If the cell has ID url, the code changes the Treeview's cursor to hand2. If the mouse if not over a cell or is not over the url column, the code restores the cursor to the default.

Handling Mouse Clicks

When the user presses and releases the mouse on the Treeview, the following event handler executes.

def mouse_up(self, event): # Open the link. if get_cell_id(self.tree, event.x, event.y) == 'url': item_id = self.tree.identify_row(event.y) if item_id: url = self.tree.set(item_id, 'url') webbrowser.open(url)

Like the mouse_move event handler, this code calls the get_cell_id helper function to see if the mouse is over a URL cell. If it is, the code calls identify_row to see which row was clicked. It then calls the tree's set method to get the url value from that row. (Yes, set is a stupid name for a method that gets values. The set method can both get and set values depending on how many arguments you give it. That's poor software engineering because every method should have a single well-define purpose, but here we are.)

Anyway, after it gets the url value, the code passes it to the webbrowser.open function to open the URL in the system's default browser.

Note that this code has no error handling so, if the value isn't a valid URL, something weird will probably happen. On my system, Microsoft Edge opens and displays an error page, even though Edge is not my default browser. Annoyingly, you can't uninstall Edge from Windows no matter how much you hate it.

Conclusion

This example shows how you can let the user click on a URL cell to open the URL in a browser. The Treeview doesn't let you style different cells in the same row differently so it's not a perfect solution, but it's relatively easy as long as you're not too fussy about style.

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

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