Python Tkinter ListBox

By | April 10, 2020

In this tutorial, we will learn about Python Tkinter ListBox. Tkinter ListBox helps up to show a list of items in application and from this list, user can select multiple options.

Syntax to add Python Tkinter ListBox:

w = ListBox (master, options)   
  • master: This represents the parent window.
  • optionsHere is the list of most commonly used options for this widget.

Example Program:

from tkinter import *  
 
mainWindow = Tk()  

mainWindow.geometry("300x150")

listBox = Listbox(mainWindow, height="5")
listBox.insert(1, "Android")
listBox.insert(2, "C")
listBox.insert(3, "Java")
listBox.insert(4, "PHP")
listBox.insert(5, "Python")
listBox.insert(6, "Ruby")
listBox.pack()

mainWindow.mainloop() 

Output:

Python Tkinter Listbox

Various possible options in Python Tkinter ListBox:

OptionDescription
bgThis option helps us to set normal background color of widget.
bdThis option helps us to set the border size around widget.
cursorThis option helps us to set the style of cursor like an arrow, dot etc
fontThis option helps us to set the style of font.
fgThis option helps us to set normal foreground colour of widget.
heightThis option helps us to set height of widget.
highlightcolorThis option is used to set color of list item at time of widget in focus.
highlightthicknessThis option is used to set the thickness of highlight.
reliefThis helps us to set the style of the border by which is default Flat.
selectbackgroundThis option is used to set background color for selected text.
selectmodeThis options helps us to set selectmode of options means how many item a user can select in single time.  This option can be set to BROWSE, SINGLE, MULTIPLE, EXTENDED.
widthThis helps us to set the width of the widget.
xscrollcommandThis option used to let the user scroll the Listbox horizontally.
yscrollcommandThis option used to let the user scroll the Listbox vertically.

Python Tkinter ListBox Functions:

There are various Python Tkinter ListBox functions available. Which makes it so easy to use entry widget in our application these functions are as follow.

FunctionDescription
activate ( index )This function helps us to select lines at the specified index in function.
curselection()This function returns tuple with numbers of selected lines in ListBox. In case of nothing selected it will return empty tuple.
delete(first, last = None)This function is used to delete lines between the mentioned index range.
get(first, last = None)This function is used to get all the items with falls between the mentioned index range.
index(i)This function is used to place line from the specified index at the top of the widget.
insert(index, *elements)This function is used to add new lines before the specified index.
see(index)This function is used to adjust the postion for ListBox to make sure the line at given index is visible.
size()This function returns the total number of lines in ListBox widget.
xview()This function is used to make ListBox widget horizontally scrollable.
yview()This function is used to make ListBox widget vertically scrollable.