";require "../templates/head_jq_bs4.php";echo "";echo "
";$img_path="..";require "top-link-tkinter.php";require "templates/top_bs4.php"; echo "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.
# Import necessary librariesimport tkinter as tkfrom tkinter import ttk
# Initialize Tkinter windowroot = tk.Tk()root.title("Treeview Multi-Select Example")root.geometry("600x400")
# 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)
# 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"
# Bind click event to the Treeviewtree.bind("<Button-1>", on_row_click)
# 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()
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.
Author
🎥 Join me live on YouTubePassionate 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.