How to disable multiselection on Treeview in tkinter?
When using the tkinter library in Python, the Treeview widget is a useful tool for displaying hierarchical data in a graphical form. However, by default, users can select multiple items in the Treeview widget, which can be inconvenient for certain applications. In this article, we will explore how to disable multiselection on the Treeview widget using tkinter.
To begin, let’s create a simple Treeview widget with sample data using the following code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
# create sample data
data = {"Animal": {"Mammal": ["Dog", "Cat", "Horse"], "Reptile": ["Snake", "Lizard", "Turtle"]},
"Plant": {"Flower": ["Rose", "Daisy", "Lily"], "Tree": ["Oak", "Maple", "Pine"]}}
# create Treeview widget
tree = ttk.Treeview(root)
tree.pack()
# loop through data and insert into tree
for key, value in data.items():
parent = tree.insert("", "end", text=key)
for subkey, subvalue in value.items():
child = tree.insert(parent, "end", text=subkey)
for item in subvalue:
tree.insert(child, "end", text=item)
root.mainloop()
This code creates a Treeview widget with hierarchical data consisting of animals and plants, with subcategories of mammals, reptiles, flowers, and trees. Each subcategory has a list of items.
By default, when we run this code, we can select multiple items in the Treeview widget by holding down the Ctrl or Shift keys and clicking on the items. To disable this functionality, we need to make use of the selectmode parameter of the Treeview widget.
The selectmode parameter controls the selection mode of the Treeview widget and can take one of three values:
- “browse”: Allows only a single item to be selected at a time. This is the default value.
- “extended”: Allows multiple items to be selected by dragging the mouse or holding down the Shift or Ctrl keys.
- “none”: Disallows any item to be selected.
To disable multiselection in the Treeview widget, we simply set the selectmode parameter to “browse”, as shown below:
tree = ttk.Treeview(root, selectmode="browse")
Now, if we run the updated code and try to select multiple items in the Treeview widget, we find that we are no longer able to do so. Only a single item can be selected at a time, and the selection is mutually exclusive.
In some cases, we may want to temporarily allow multiselection on the Treeview widget, and then revert back to single selection. To do this, we can make use of the tag_bind() method of the Treeview widget.
The tag_bind() method binds a particular binding tag to a certain event, such as a mouse click or keyboard keypress. In the case of the Treeview widget, the binding tag is “Treeview” by default, and we can use this tag to catch mouse clicks on the widget.
To allow temporary multiselection on the Treeview widget, we can bind the Ctrl and Shift keys to the “Treeview” tag using the following code:
def enable_multiselect():
tree.tag_bind("Treeview", "<Control-Button-1>", lambda event: "break")
tree.tag_bind("Treeview", "<Shift-Button-1>", lambda event: "break")
# call this function to enable temporary multiselection
enable_multiselect()
This code creates a function called enable_multiselect(), which binds the Ctrl and Shift keys to the “Treeview” tag and prevents the default behavior of selecting multiple items.
We can then call this function whenever we need to enable temporary multiselection on the Treeview widget, and remove the bindings when we are ready to revert back to single selection:
def disable_multiselect():
tree.tag_unbind("Treeview", "<Control-Button-1>")
tree.tag_unbind("Treeview", "<Shift-Button-1>")
# call this function to disable temporary multiselection
disable_multiselect()
This code creates a function called disable_multiselect(), which removes the Ctrl and Shift key bindings from the “Treeview” tag and reverts back to single selection mode.
Conclusion
In conclusion, the Treeview widget in tkinter is a powerful tool for displaying hierarchical data, but it can be inconvenient to allow multiple items to be selected by default. To disable multiselection in the Treeview widget, we can set the selectmode parameter to “browse” when creating the widget. To allow temporary multiselection, we can use the tag_bind() method tobind the Ctrl and Shift keys to the “Treeview” tag and then remove the bindings when we no longer need them.
By following these simple steps, we can customize the selection behavior of the Treeview widget to suit our needs and create a more user-friendly interface for our applications.