Python

Python Tkinter Listbox: A Comprehensive Guide

Python Tkinter Listbox
Python Tkinter Listbox

Python Tkinter Listbox

When developing desktop applications with Python, Tkinter offers several widgets for creating interactive graphical interfaces. The Listbox is one of the useful widgets when an application needs to present a group of values in a structured and selectable format.

A Listbox displays multiple entries inside a dedicated area of the GUI. Users can interact with these entries and make selections according to the selection mode configured by the developer. Depending on the application requirement, it can support either one selected item or multiple selected items.

Python Tkinter Listbox: A Comprehensive Guide

Syntax for Creating a Listbox

The basic syntax used to create a Listbox widget is:

w = Listbox(parent, options)

In this syntax, parent represents the window or container where the Listbox will be placed. The options parameter contains configuration values that control the widget’s appearance and functionality.

Using these properties, developers can modify the Listbox’s dimensions, colors, font, border style, selection behavior, and scrolling features.

Listbox Options

OptionDescription
bgDetermines the background shade used for the Listbox area.
bdControls how wide the Listbox border appears; its default setting is 2 pixels.
cursorSpecifies the pointer style shown when the mouse is placed over the Listbox.
fontSets the typeface and text appearance used for the entries.
fgControls the color in which Listbox text is displayed.
heightDetermines how many text rows can be visible within the Listbox at once. The default is 10.
highlightcolorDefines the color of the focus indicator surrounding the Listbox.
highlightthicknessSets the thickness of the border shown around the widget when it receives focus.
reliefControls the visual style of the Listbox border, with SUNKEN being the default style.
selectbackgroundSpecifies the background color that appears behind an item after it has been selected.
selectmodeDetermines the selection behavior and supports modes such as BROWSE, SINGLE, MULTIPLE, and EXTENDED.
widthSets the horizontal size of the Listbox according to the number of characters it can accommodate.
xscrollcommandConnects the Listbox with a horizontal scrollbar for moving through wider content.
yscrollcommandConnects the Listbox with a vertical scrollbar for navigating through longer lists.

Listbox Methods

Configuration options control how the widget looks and behaves, while Listbox methods are used to perform operations on its entries. These methods make it possible to manage the contents of the widget directly through Python code.

MethodDescription
activate(index)Activates the Listbox item located at the specified index.
curselection()Returns a tuple containing the index or indexes the currently selected entries.
delete(first, last=None)Removes one item or range of items from the Listbox.
get(first, last=None)Obtains the value of one item or multiple items from the specified range.
index(i)Returns the index associated with the specified Listbox item.
insert(index, elements)Adds one or more new entries at the specified position.
nearest(y)Finds the index of the entry located closest to the supplied y-coordinate.
see(index)Moves the Listbox view so that the specified entry becomes visible.
size()Returns the total number of entries currently present in the Listbox.
xview()Provides access to the horizontal viewing position of the Listbox.
xview_moveto(fraction)Moves the horizontal view according to the specified fraction.
xview_scroll(number, what)Moves the horizontal view by the specified number of scrolling units.
yview()Provides access to the vertical viewing position of the Listbox.
yview_moveto(fraction)Moves the vertical view to a specified fraction of the available area.
yview_scroll(number, what)Moves the Listbox vertically by the requested number of units.

Example 1: Creating a Simple Listbox

The first example creates a basic Listbox and places four country names inside it. The insert() method is used to add each entry.

from tkinter import *

top = Tk()
top.geometry("200x250")

lbl = Label(top, text="A list of favorite countries...")

listbox = Listbox(top)

listbox.insert(1, "India")
listbox.insert(2, "USA")
listbox.insert(3, "Japan")
listbox.insert(4, "Australia")

lbl.pack()
listbox.pack()

top.mainloop()

Output

After executing the program, the application window contains a Listbox with four entries: India, USA, Japan, and Australia. The user can click an entry to select it.

This simple example demonstrates the basic process of creating a Listbox, adding values to it, and placing the widget inside the Tkinter window.

Example 2: Deleting an Active Item from the List

The second example demonstrates how an existing Listbox item can be removed. A Delete button is connected to the delete() method so that the currently active entry can be removed from the list.

from tkinter import *

top = Tk()
top.geometry("200x250")

lbl = Label(top, text="A list of favorite countries...")

listbox = Listbox(top)

listbox.insert(1, "India")
listbox.insert(2, "USA")
listbox.insert(3, "Japan")
listbox.insert(4, "Australia")

# Button to delete selected item
btn = Button(
    top,
    text="Delete",
    command=lambda: listbox.delete(ANCHOR)
)

lbl.pack()
listbox.pack()
btn.pack()

top.mainloop()

Output

The GUI displays the same list of countries along with a Delete button. When the user selects an entry and presses the button, the selected item is removed from the Listbox.

In this example, ANCHOR identifies the active Listbox entry, while delete() removes that entry from the widget.

Frequently Asked Questions

1. What is a Listbox in Python Tkinter?

A Listbox is a Tkinter widget used to present multiple values in a selectable list inside a graphical user interface.

2. How do I add an item to a Tkinter Listbox?

The insert() method is used to place a new entry into the Listbox at a specified position.

3. How can I remove an item from a Listbox?

The delete() method can be used to remove an individual entry or a range of entries.

4. How can I get the selected item from a Listbox?

The curselection() method can identify the selected item’s index, while get() can be used to obtain its actual value.

5. Can a Tkinter Listbox support multiple selections?

Yes. The selectmode option provides selection modes such as MULTIPLE and EXTENDED, which allow multiple entries to be selected.

6. Can a Listbox be connected to a scrollbar?

Yes. The xscrollcommand and yscrollcommand options can be used to connect horizontal and vertical scrolling controls with a Listbox.

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here
YT:- DecodeIT

Conclusion

The Python Tkinter Listbox is a practical widget for displaying and managing collections of selectable values in desktop applications. Its configuration options allow developers to control the widget’s appearance, dimensions, selection behavior, and scrolling features.

With methods such as insert(), delete(), get(), curselection(), and size(), developers can easily manage Listbox contents through Python code. Whether you are creating a basic selection interface or a more interactive desktop application, understanding the Listbox provides a useful foundation for working with selectable data in Tkinter.

Keywords: Python Tkinter Listbox, python tkinter listbox select item, python tkinter listbox scrollbar, python tkinter listbox with checkboxes, python tkinter listbox example, python tkinter listbox multiple columns, python tkinter listbox clear, python tkinter listbox width, python tkinter listbox scrollbar grid, python tkinter listbox multiple selection, tkinter listbox get selected item, python tkinter listbox add item, tkinter listbox multiple columns, tkinter listbox get all items, python tkinter listbox tutorial, tkinter listbox methods

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us