";require "../templates/head_jq_bs4.php";echo "";echo "";$img_path="..";require "top-link-tkinter.php";require "templates/top_bs4.php"; echo "

Multi-Row Selection in Tkinter Treeview Without Holding Ctrl Key

";require "templates/body_start.php";?>
Multi-Row Selection in Tkinter Treeview

Introduction

By default, Tkinter Treeview allows multiple row selection only when holding the Ctrl key. In this tutorial, we modify the behavior so that users can select multiple rows just by clicking, without holding any key.

1. Importing Required Modules

# Import necessary librariesimport tkinter as tkfrom tkinter import ttk

2. Creating the Main Application Window

# Initialize Tkinter windowroot = tk.Tk()root.title("Treeview Multi-Select Example")root.geometry("600x400")

3. Creating the Treeview Widget

# Define Treeview with column headingstree = ttk.Treeview(root, columns=("Name", "Age"), show="headings", selectmode="extended")tree.heading("Name", text="Name")tree.heading("Age", text="Age")# Insert sample datadata = [("Alice", 25), ("Bob", 30), ("Charlie", 22)]for person in data:    tree.insert("", "end", values=person)tree.pack(pady=20)

4. Custom Selection Logic

# Function to allow row selection without Ctrl keydef on_row_click(event):    item = tree.identify_row(event.y) # Get clicked row    if item:        if item in tree.selection():            tree.selection_remove(item)  # Deselect if already selected        else:            tree.selection_add(item)  # Add row to selection    return "break"

5. Binding Click Event

# Bind click event to the Treeviewtree.bind("<Button-1>", on_row_click)

6. Running the Application

# Run the Tkinter event looproot.mainloop()


import tkinter as tkfrom tkinter import ttkdef on_row_click(event):    """ Allow multiple row selection without holding Ctrl key """    item = tree.identify_row(event.y)  # Get row under cursor    if item:        if item in tree.selection():            tree.selection_remove(item)  # Toggle off selection if already selected        else:            tree.selection_add(item)  # Add new row to selection    return "break"  # Prevent default Tkinter selection behavior# Create main windowroot = tk.Tk()root.title("Multi-Select Treeview Without Ctrl key")# Create Treeview with 'extended' mode (allows multiple selection)tree = ttk.Treeview(root, columns=("Name", "Age"),     show="headings", selectmode="extended")tree.heading("Name", text="Name")tree.heading("Age", text="Age")# Insert some rowsdata = [("Alice", 25), ("Bob", 30), ("Charlie", 22), ("David", 28), ("Emma", 35)]for person in data:    tree.insert("", "end", values=person)tree.pack(padx=20, pady=20, fill="both", expand=True)# Bind single-click event to prevent default selection clearingtree.bind("<Button-1>", on_row_click)root.mainloop()

Conclusion

Now, users can select multiple rows **just by clicking**, without needing the Ctrl key. This approach is useful for interactive applications where users frequently select multiple items.


Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer