Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to disable multiselection on Treeview in tkinter?
The Treeview widget displays hierarchical data in columns and rows. By default, it allows multiple item selection, but you can disable this by setting selectmode="browse" to allow only single selection.
Syntax
ttk.Treeview(parent, selectmode="browse", **options)
Parameters
The selectmode parameter accepts these values ?
-
"extended"? Default mode, allows multiple selection -
"browse"? Single selection mode -
"none"? Disables all selection
Example
Here's how to create a Treeview with single selection mode ?
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x300")
# Create an instance of Style widget
style = ttk.Style()
style.theme_use('clam')
# Add a Treeview widget and set the selection mode
tree = ttk.Treeview(win, columns=("c1", "c2"), show='headings', height=8, selectmode="browse")
tree.column("#1", anchor=CENTER, stretch=NO)
tree.heading("#1", text="Fname")
tree.column("#2", anchor=CENTER, stretch=NO)
tree.heading("#2", text="Lname")
# Insert the data in Treeview widget
tree.insert('', 'end', text="1", values=('Alex', 'M'))
tree.insert('', 'end', text="2", values=('Belinda', 'Cross'))
tree.insert('', 'end', text="3", values=('Ravi', 'Malviya'))
tree.insert('', 'end', text="4", values=('Suresh', 'Rao'))
tree.insert('', 'end', text="5", values=('Amit', 'Fernandiz'))
tree.insert('', 'end', text="6", values=('Raghu', 'Sharma'))
tree.insert('', 'end', text="7", values=('David', 'Nash'))
tree.insert('', 'end', text="8", values=('Ethan', 'Plum'))
tree.pack()
win.mainloop()
Output
The code displays a Treeview widget where you can select only one item at a time. Multi−selection is disabled.
Key Points
-
selectmode="browse"enables single selection mode - Users can select only one item at a time
- Clicking another item deselects the previous selection
- Use
selectmode="none"to completely disable selection
Conclusion
Set selectmode="browse" in the Treeview constructor to disable multi−selection. This ensures users can only select one item at a time, providing better control over user interactions.
Advertisements
