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.
  • options: Here 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() 

Various possible options in Python Tkinter ListBox:

Option Description
bg This option helps us to set normal background color of widget.
bd This option helps us to set the border size around widget.
cursor This option helps us to set the style of cursor like an arrow, dot etc
font This option helps us to set the style of font.
fg This option helps us to set normal foreground colour of widget.
height This option helps us to set height of widget.
highlightcolor This option is used to set color of list item at time of widget in focus.
highlightthickness This option is used to set the thickness of highlight.
relief This helps us to set the style of the border by which is default Flat.
selectbackground This option is used to set background color for selected text.
selectmode This 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.
width This helps us to set the width of the widget.
xscrollcommand This option used to let the user scroll the Listbox horizontally.
yscrollcommand This 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.

METHOD DESCRIPTION
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
Scroll to Top
×