Python tkinter Listbox

A list box is useful to display a list of items in a box so that the user can select 1 or more items.

tkinter Listbox

To create a list box, we have to create an object of Listbox class, as:

Syntax

CopiedCopy Code
lb = Listbox(f, font="Arial 12 bold", fg='blue', bg='yellow', height=8, width=24, activestyle='underline', selectmode=MULTIPLE)

Here, 'lb' is the list box object.The option 'height' represents the number of lines shown in the list box.'width' represents the width of the list box in terms of number of characters and the default is 20 characters.The option 'activestyle' indicates the appearance of the selected item.It may be 'underline', 'dotbox' or 'none'.The default value is 'underline'.The option 'selectmode' may take any of the following values:

  1. BROWSE : Normally, we can select one item (or line) out of a list box.If we click on an item and then drag to a different item, the selection will follow the mouse.This is the default value of 'selectmode' option.
  2. SINGLE : This represents that we can select only one item( or line) from all available list of items.
  3. MULTIPLE : We can select 1 or more number of items at once by clicking on the items. If an item is already selected, clicking second time on the item will un-select it.
  4. EXTENDED : We can select any adjacent group of items at once by clicking on the first item and dragging to the last item.

Once the list box is created, we should insert items into the list box using insert() method as:

CopiedCopy Code
lb.insert(0, 'Standford University')
						lb.insert(1, ' Oxford University')

To bind the ListboxSelect event with a method, we can use the bind() method as:

CopiedCopy Code
lb.bind('<<ListboxSelect>>', on_select)

The meaning of the previous statement is that when the user selects any items in the list box, the method on_select() will be called.This method can be written something like this:

CopiedCopy Code

def on_select(event): 
   #create an empty list box 
   lst = [] 
   #know the indexes of the selected items 
   indexes = lb.curselection() 
   #retrieve the items names depending on indexes 
   #append the items names to the list box 
   for i in indexes: 
      lst.append(lb.get(i))

tkinter Listbox example

A Python program to create a list box with Universities names and display the selected Universities names in a text box

CopiedCopy Code

from tkinter import * 
class ListboxDemo: 
   def __init__(self, root): 
      self.f = Frame(root, width=700, height=400) 
      self.f.propagate(0) 
      self.f.pack() 
      self.lbl = Label(self.f, text="Click one or more of the Universities below:", font="Calibri 14") 
      self.lbl.place(x=50, y=50) 
      self.lb = Listbox(self.f, font="Arial 12 bold", fg='blue', bg='yellow', height=8, selectmode=MULTIPLE) 
      self.lb.place(x=50, y=100)
      for i in ["Standford University", "Oxford University", "Texas AMUniversity", "Cambridge University", "University of California"]:
         self.lb.insert(END, i) 
#bind the ListboxSelect event to on_select() method   
         self.lb.bind('ListboxSelect', self.on_select) 
#create text box to display selected items 
         self.t = Text(self.f, width=40, height=6, wrap=WORD) self.t.place(x=300, y=100) 
   def on_select(self, event): 
      self.lst = [] 
      indexes = self.lb.curselection()

#retrieve the items names depending on indexes 
#append the items names to the list box 
for i in indexes: 
   self.lst.append(self.lb.get(i)) 
#delete the previous content of the text box 
   self.t.delete(0.0, END) 
#insert the new contents into the text box 
   self.t.insert(0.0, self.lst) 
root = Tk() 
root.title("List box demonstration.") 
obj = ListboxDemo(root)
root.mainloop()