Use Tkinter to create multiple selections

Prerequisites: Python Tkinter – ListBox widget, Scrollable list box in Python-tkinter

Tkinter is a GUI library in python that is easy to read and understand. In Tkinter, multiple selections can be done using the List Box widget. Usually, a list box displays different items in the form of a list. The list box widget provides a selection of one or more items in the list. There are a number of options available in the list box widget, allowing the user to select multiple options. By assigning the selection mode options to multiple, the user can select more than one option. If the selection mode option is Single, the user can only select one option.

The selectmode option of the list box widget can be single, browse, multiple, or extended.

  • Single – Select a line of text.
  • Browse – This is the default option and the user can select a line of text.
  • Multi – Select multiple lines of text without dragging from the first line of the option to the last line.
  • Extended – Users can select and drag adjacent multiple lines of text.

Syntax:

list_box = Listbox(root, options, ....)

Example 1: A Python program that displays a limited number of items in a list box.

# Python program demonstrating
# Multiple selection in Listbox widget
  
  
from tkinter import * 
  
window = Tk()
window.geometry( '100x150' )
  
# Choosing selectmode as multiple 
# for selecting multiple options
list = Listbox(window, selectmode = "multiple" )
  
# Widget expands horizontally and
# vertically by assigning both to 
# fill option
list .pack(expand = YES, fill = "both" )
  
# Taking a list 'x' with the items 
# as languages
x = [ "C" , "C++" , "Java" , "Python" , "R" , "Go" , "Ruby" , "JavaScript" , "Swift" ]
  
for each_item in range ( len (x)):
      
     list .insert(END, x[each_item])
      
     # coloring alternative lines of listbox
     list .itemconfig(each_item, bg = "yellow" if each_item % 2 = = 0 else "cyan" )
      
window.mainloop()

Output:

Users can select multiple options from the multiple picklist boxes above. Because the number of items in the list box is limited and cannot accommodate the specified size, the user can view all the items. However, if there are more items to display to the user, all of them won’t be visible in the list box at once. Therefore, if there are more items to display in the list, you must attach a scroll bar to the list box. This can be done via the yscroll command option in the list box (vertical scrolling).

Example 2: The Python program displays a list box with additional scrollbars.

# Python program demonstrating Multiple selection
# in Listbox widget with a scrollbar
  
  
from tkinter import *
  
window = Tk()
window.title( 'Multiple selection' )
  
# for scrolling vertically
yscrollbar = Scrollbar(window)
yscrollbar.pack(side = RIGHT, fill = Y)
  
label = Label(window, text = "Select the languages below :  " , font = ( "Times New Roman" , 10 ), padx = 10 , pady = 10 )
label.pack()
list = Listbox(window, selectmode = "multiple" , yscrollcommand = yscrollbar. set )
  
# Widget expands horizontally and 
# vertically by assigning both to
# fill option
list .pack(padx = 10 , pady = 10 , expand = YES, fill = "both" )
  
x = [ "C" , "C++" , "C#" , "Java" , "Python" , "R" , "Go" , "Ruby" , "JavaScript" , "Swift" , "SQL" , "Perl" , "XML" ]
  
for each_item in range ( len (x)):
      
     list .insert(END, x[each_item])
     list .itemconfig(each_item, bg = "lime" )
  
# Attach listbox to vertical scrollbar
yscrollbar.config(command = list .yview)
window.mainloop()

Output:

Watch out for freaks! Consolidate your foundational Python programming fundamentals course and learn the fundamentals.

First, your interview preparation enhances your data structure concepts with the Python DS course.