How to disable multiselection on Treeview in tkinter?

TkinterPythonGUI-Programming

The Treeview widget is used to display a list of items with more than one feature in the form of columns. By default, the listed items in a Treeview widget can be selected multiple times, however you can disable this feature by using selectmode="browse" in the Treeview widget constructor. The Treeview widget can be implemented by using the Treeview(root, column, **options) constructor.

Example

The following example demonstrates how to disable multiselection in a Treeview widget.

# 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, column=("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= "5",values=('Raghu','Sharma'))
tree.insert('', 'end',text= "5",values=('David','Nash'))
tree.insert('', 'end',text= "5",values=( 'Ethan','Plum'))

tree.pack()

win.mainloop()

Output

Running the above code will display a treeview widget with a list of items. You can select only a single item at a time from the list.

raja
Updated on 16-Dec-2021 11:04:14

Advertisements