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) This represents the parent window. master: Here is the list of most commonly used options for this widget. options: Example Program: from tkinter import *
mainWindow = Tk()
listBox = Listbox(mainWindow, height="5")
mainWindow.mainloop() Output: 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 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.
Function Description 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.