How to correctly select multiple items with the mouse in Tkinter Treeview?

The Tkinter Treeview widget provides a table-like interface for displaying hierarchical data. By default, it allows single-row selection, but you can enable multiple row selection to let users select several items simultaneously using mouse interactions.

Enabling Multiple Selection

To enable multiple item selection in a Treeview, you need to configure the selectmode parameter. The default mode is 'browse' (single selection), but you can change it to 'extended' for multiple selection −

import tkinter as tk
from tkinter import ttk

# Create main window
root = tk.Tk()
root.geometry("700x350")
root.title("Treeview Multiple Selection")

# Create Treeview with multiple selection enabled
tree = ttk.Treeview(root, 
                    columns=("c1", "c2", "c3"), 
                    show='headings', 
                    height=8,
                    selectmode='extended')  # Enable multiple selection

# Configure columns
tree.column("c1", anchor=tk.CENTER, width=100)
tree.heading("c1", text="ID")
tree.column("c2", anchor=tk.CENTER, width=150)
tree.heading("c2", text="First Name")
tree.column("c3", anchor=tk.CENTER, width=150)
tree.heading("c3", text="Last Name")

# Insert sample data
data = [
    ('1', 'Joe', 'Nash'),
    ('2', 'Emily', 'Mackmohan'),
    ('3', 'Estilla', 'Roffe'),
    ('4', 'Percy', 'Andrews'),
    ('5', 'Stephan', 'Heyward'),
    ('6', 'Alice', 'Johnson'),
    ('7', 'Bob', 'Williams')
]

for row in data:
    tree.insert('', 'end', values=row)

tree.pack(pady=20)

root.mainloop()

Mouse Selection Methods

Once selectmode='extended' is set, users can select multiple items using these mouse interactions −

  • Ctrl + Click: Select/deselect individual items while keeping existing selections
  • Shift + Click: Select a range of items from the last selected item to the clicked item
  • Single Click: Select one item and clear previous selections

Getting Selected Items

You can retrieve all selected items using the selection() method −

import tkinter as tk
from tkinter import ttk

def show_selection():
    selected_items = tree.selection()
    print(f"Selected {len(selected_items)} items:")
    for item in selected_items:
        values = tree.item(item)['values']
        print(f"  ID: {values[0]}, Name: {values[1]} {values[2]}")

root = tk.Tk()
root.geometry("700x400")

# Treeview with multiple selection
tree = ttk.Treeview(root, 
                    columns=("c1", "c2", "c3"), 
                    show='headings', 
                    selectmode='extended')

# Configure columns
tree.column("c1", anchor=tk.CENTER, width=100)
tree.heading("c1", text="ID")
tree.column("c2", anchor=tk.CENTER, width=150)
tree.heading("c2", text="First Name")
tree.column("c3", anchor=tk.CENTER, width=150)
tree.heading("c3", text="Last Name")

# Insert data
data = [
    ('1', 'Joe', 'Nash'),
    ('2', 'Emily', 'Mackmohan'),
    ('3', 'Estilla', 'Roffe'),
    ('4', 'Percy', 'Andrews'),
    ('5', 'Stephan', 'Heyward')
]

for row in data:
    tree.insert('', 'end', values=row)

# Button to show selected items
btn = tk.Button(root, text="Show Selected Items", command=show_selection)

tree.pack(pady=10)
btn.pack(pady=10)

root.mainloop()

Selection Modes Comparison

Mode Selection Type Mouse Behavior
'browse' Single (default) Click to select one item
'extended' Multiple Ctrl+Click, Shift+Click for multiple
'none' No selection Cannot select items

Conclusion

Set selectmode='extended' in your Treeview to enable multiple selection. Users can then select multiple items using Ctrl+Click for individual selection or Shift+Click for range selection. Use tree.selection() to retrieve all selected items.

Updated on: 2026-03-25T22:23:29+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements