Python Tkinter ListBox

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 Methods:

After learning about various available options in Python Tkinter ListBox. Its time to check out some available methods for Python Tkinter ListBox widget.

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