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.
from tkinter import *
mainWindow = Tk()
listBox = Listbox(mainWindow, height="5")
Various possible options in Python Tkinter ListBox:
||This option helps us to set normal background color of widget.
||This option helps us to set the border size around widget.
||This option helps us to set the style of cursor like an arrow, dot etc
||This option helps us to set the style of font.
||This option helps us to set normal foreground colour of widget.
||This option helps us to set height of widget.
||This option is used to set color of list item at time of widget in focus.
||This option is used to set the thickness of highlight.
||This helps us to set the style of the border by which is default Flat.
||This option is used to set background color for selected text.
||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.
||This helps us to set the width of the widget.
||This option used to let the user scroll the Listbox horizontally.
||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.
|activate ( index )
||This method helps us to select lines at the specified index in the method.
||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.
||This method is used to place a line from the specified index at the top of the widget.
||This method is used to add new lines before the specified index.
||This method is used to adjust the position for ListBox to make sure the line at given index is visible.
||This method returns the total number of lines in the ListBox widget.
||This method is used to make ListBox widget horizontally scrollable.
||This method is used to make the ListBox widget vertically scrollable.