Tkinter Listbox

Tkinter Listbox widget provides a list of options to a user in Python GUI. List Box displays all of its options at once where all options are in text format.

Syntax:

The Parameter master represents the parent window and options can be used as key-value pairs separated by commas.

Options of Tkinker Listbox:

fg: Show the color used for the text.

font: Font used for the text in the listbox.

highlightthickness: Shows the thickness of the focus highlight.

selectbackground: Background color for text that has been selected.

bg: Shows the background color for the area around the widget.

cursor: Cursor that appears when the mouse is over the listbox.

yscrollcommand: This listbox widget can be linked to a vertical scrollbar to allow the user to scroll the listbox vertically. .

xscrollcommand: Link this listbox widget to a horizontal scrollbar to allow the user to scroll the listbox horizontally.

bd: Show the size of the border around the indicator. By default is 2 pixels.

width: Shows the width of the widget in characters. The default is 20.

height: It shows the number of lines shown in the listbox. By default is 10.

selectmode: This option determines the number of items that can be selected from the list and it also shows the effects of the mouse on this selection. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.

Methods of Tkinter Listbox:

size(): This method returns the number of lines in the listbox.

see ( index ):This method adjust the position of the listbox which the line referred to by index is visible.

activate ( index ): This method selects the line specifies by the given index.

index ( i ): This method is used to position the visible part of the listbox in which the line containing index i is at the top of the widget.

xview(): This method used to make the listbox horizontally scrollable.

yview(): This method used to make the listbox vertically scrollable.

nearest ( y ): This method return the index of the visible line closest to the y-coordinate y relative to this widget.

xview_moveto ( fraction ): This method scroll the listbox so that the leftmost fraction of the width of its longest line is outside the left side of the listbox. Fraction is in the range from 0 to1.

yview_moveto ( fraction ): This method scroll the listbox so that the top fraction of the width of its longest line is outside the left side of the listbox. Fraction is in the range from 0 to 1.

xview_scroll ( number, what ): This method scrolls the listbox horizontally. The number argument tells how many to scroll.

yview_scroll ( number, what ): This method scrolls the Listbox vertically. The number argument tells how many to scroll.

delete ( first, last=None ): This method selects the lines whose indices are in the range from first to last. The single line with index first is deleted, if the second argument is omitted.

curselection(): This method returns a tuple containing the line numbers of the selected elements, start from 0. Returns an empty tuple if nothing is select.

Example:
from tkinter import *

appWindow = Tk()
appWindow.title("Window Title-Tutorialsart.com")

appWindow.geometry('350x200')

frame1 = Frame(appWindow)
frame1.pack()

label1 = Label(appWindow, text="WEEK DAYS", bg="orange")
label1.pack()

listbox = Listbox(appWindow)

listbox.insert(1, "MONDAY")
listbox.insert(2, "TUESDAY")
listbox.insert(3, "WEDNESDAY")
listbox.insert(4, "THURSDAY")

listbox.pack()


appWindow.mainloop()
Output:
07 tkinter listbox

Comments are closed.