Signup/Sign In

Python Tkinter Listbox Widget

In this tutorial, we will cover the Tkinter Listbox widget in Python which is used to display different types of items to the user in form of a List inside a box and the user can select the items.

  • The items contain the same type of font and the same font color.

  • It is important to note here that only text items can be placed inside a Listbox widget.

  • From this list of items, the user can select one or more items according to the requirements.

Tkinter Listbox Widget

The syntax of the Tkinter Listbox widget is given below:

W = Listbox(master, options)   

In the above syntax, the master parameter denotes the parent window. You can use many options to change the look of the ListBox and these options are written as comma-separated key-value pairs.

Tkinter Listbox Widget Options:

Following are the various options used with Listbox widgets:

Name of Option Description
bg This option indicates the background color of the widget.
bd This option is used to represent the size of the border. The default value is 2 pixels.
cursor With the help of this option, the mouse pointer will look like the cursor type like dot, arrow, etc.
font This option indicates the font type of the Listbox items.
fg This option indicates the color of the text.
height This option is used to represents the count of the lines shown in the Listbox. The default value of this option is 10.
highlightcolor This option is used to indicate the color of the Listbox items when the widget is under focus.
highlightthickness This option is used to indicate the thickness of the highlight.
relief This option indicates the type of border. The default value is SUNKEN.
selectbackground This option is used to indicate the background color that is used to display the selected text.
selectmode This option is used to determine the number of items that can be selected from the list. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.
width This option is used to represent the width of the widget in characters.
xscrollcommand This option is used to let the user scroll the Listbox horizontally.
yscrollcommand This option is used to let the user scroll the Listbox vertically.

Tkinter ListBox Widget Methods:

Following are the methods associated with the Listbox widget:

Method Description
activate(index) This method is mainly used to select the lines at the specified index.
curselection() This method is used to return a tuple containing the line numbers of the selected element or elements, counting from 0. If nothing is selected, return an empty tuple.
delete(first, last = None) This method is used to delete the lines which exist in the given range.
get(first, last = None) This method is used to get the list of items that exist in the given range.
index(i) This method is used to place the line with the specified index at the top of the widget.
insert(index, *elements) This method is used to insert the new lines with the specified number of elements before the specified index.
nearest(y) This method is used to return the index of the nearest line to the y coordinate of the Listbox widget.
see(index) This method is used to adjust the position of the Listbox to make the lines specified by the index visible.
size() This method returns the number of lines that are present in the Listbox widget.
xview() This method is used to make the widget horizontally scrollable.
xview_moveto(fraction) This method is used to make the Listbox horizontally scrollable by the fraction of the width of the longest line present in the Listbox.
xview_scroll(number, what) This method is used to make the listbox horizontally scrollable by the number of characters specified.
yview() This method allows the Listbox to be vertically scrollable.
yview_moveto(fraction) This method is used to make the listbox vertically scrollable by the fraction of the width of the longest line present in the Listbox.
yview_scroll (number, what) This method is used to make the listbox vertically scrollable by the number of characters specified.

Tkinter ListBox Widget Example

Below we have a basic example using this widget:

from tkinter import *

top = Tk()

top.geometry("200x250")

lbl = Label(top, text="List of Programming Languages")

listbox = Listbox(top)

listbox.insert(1,"Python")

listbox.insert(2, "Java")

listbox.insert(3, "C")

listbox.insert(4, "C++")

lbl.pack()
listbox.pack()
  
top.mainloop()


In the code example above, we have created a simple Listbox widget with some items along with specifying the top list item which is the heading on the Listbox widget.

Summary:

In this tutorial, we learned about the Tkinter Listbox Widget, its basic syntax, the commonly used methods for the ListBox Widget, and a code example.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight