How to disable multiselection on Treeview in Tkinter?

Published December 30, 2021

In Tkinter tutorial, the Treeview widget displays a list of items in a tabular form or more than a single feature in terms of columns. By default, a user can be able to make multiple selections multiple times. We can limit the user to making a single selection.

To disable the user from making a multiselection on the TKinter Treeview, we usually disable the feature selectmode="browse" in the widget constructor. Here, this capability will be implemented using the syntax

 

Treeview (root, column, options)

 

Example:

In this example, we have restricted the multiple selection and user can select a single  country in Asia and Europe.

 

from tkinter import *

from tkinter import ttk

win=Tk()

win.geometry("700x300")

style= ttk.Style()

style.theme_use('clam')

tree= ttk.Treeview(win, column=("c1", "c2"), show='headings', height=8, selectmode="browse")

tree.column("#1", anchor= CENTER, stretch= NO)

tree.heading("#1", text= "Asia Countries")

tree.column("#2", anchor= CENTER, stretch= NO)

tree.heading("#2", text= "Europen Countries")

tree.insert('', 'end',text= "1",values=('Singapore', 'France'))

tree.insert('', 'end',text= "2",values=( 'Bahrain','Germany'))

tree.insert('', 'end',text= "3",values=( 'Sri Lanka','Spain'))

tree.insert('', 'end',text= "4",values=('India','Romania'))

tree.insert('', 'end',text= "5",values=('Pakistan','Portugal'))

tree.insert('', 'end',text= "5",values=('China','Belgium'))

tree.insert('', 'end',text= "5",values=('Japan','Italy'))

tree.insert('', 'end',text= "5",values=( 'Nepal','Netherlands'))

tree.pack()

win.mainloop()

 

Tkinter Disable Multi selection in treeview

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

446 Views