Tkinter Listbox

In this post, I will discuss the Tkinter Listbox widget in python. I will also cover various options that we can use to customize the style and behavior of the Listbox. At last, I will also be giving you some relevant code examples. So let’s get started.

Tkinter Listbox widget is mostly used to display a list of different text items. These items should have the same font and color. Users can interact with the list by selecting one or more items.

Here is the syntax of listbox

Syntax :

l = Listbox(window, **options) 

Options :

Here is the list of options that we can use to customize the tkinter listbox widget.

activestyleSet the style of the item of Listbox when it’s active. The default is underline.
backgroundSet the background color of Listbox.
borderwidthSet the border width of the Listbox.
cursorSet the mouse cursor when it’s on listbox.
disabledforegroundSet the text color of the listbox when it’s disabled.
exportselectionSet whether a selection should also be the X selection.default is 1.
fontSet the text font for the listbox.
foregroundSet the text color for the listbox items.
heightSet the height of the listbox.
reliefSet the style of the border. default is SUNKEN.
highlightbackgroundSet the background color of listbox when it’s active.
highlightcolorSet the text color of the listbox when it’s active.
selectmodeSet the selection mode. use SINGLE, BROWSE, MULTIPLE, EXTENDED.
selectbackgroundSet the background color of the selected item.
selectborderwidthSet the border width of the selected item.
selectforegroundSet the text color of the selected item.
stateSet the state. use NORMAL(default), DISABLED.
widthSet the width of the listbox.
xscrollcommandUse this option to allow users to scroll horizontally.
yscrollcommandUse this option to allow users to scroll vertically.

Tkinter Listbox Examples

For a better understanding of the concepts let’s look at some code examples.

Example 1 : Simple Listbox

In this code example, we will display a simple Listbox with three items and a button. On button click, the selected item will be printed on the console.

from tkinter import *

window = Tk()

# Create a listbox widget
l = Listbox(window)
l.pack()

for i in ['one','two','three']:
    l.insert(END,i) # Insert the item in the listbox

def get_value():
    # Function to be called on button click

    # Get the index of selected item using the 'curseselection' method.
    selected = l.curselection()
    if selected: # If item is selected
        print("Selected Item : ",l.get(selected[0])) # print the selected item

Button(window,text = "Print Selection",command = get_value).pack()

window.mainloop()

Output :

Tkinter listbox example 1 : output

Example 2 : Run Function On Item Click

In the following code snippet, we will display a Listbox having some items in it. when the user will click on any item, a specific function will be called.

from tkinter import *

window = Tk()


def get_value(event):
    # Function to be called on item click

    # Get the index of selected item using the 'curseselection' method.
    selected = l.curselection()
    if selected: # If item is selected
        print("Selected Item : ",l.get(selected[0])) # print the selected item

# Create a listbox widget
l = Listbox(window)
l.bind('<>',get_value)
l.pack()

for i in ['one','two','three']:
    l.insert(END,i) # Insert the item in the listbox

window.mainloop()

Output :

Tkinter listbox example 2 output

Example 3 : Add Style To Listbox

In this code example, we will use the above discussed options to add styling to the listbox.

from tkinter import *

window = Tk()

# Create a listbox widget
l = Listbox(window,background = "black",foreground = "white",font = "verdana 15 bold")
l.pack()

for i in ['one','two','three']:
    l.insert(END,i) # Insert the item in the listbox

window.mainloop()

Output :

Tkinter listbox example 3 output

Example 4 : Todo App

In the final code example, we will create a simple todo app.

To understand this code example you need the knowledge of the Tkinter button and entry.

from tkinter import *

window = Tk()

def insert():
    # Python function to insert item
    l.insert(END,e.get())

def delete():
    # function to delete selected item
    s = l.curselection()
    if s:
        l.delete(s[0])

def clear():
    # function to clear listbox
    l.delete(0,END)


e = Entry(window)
e.grid(row = 0,column = 0)
Button(window,text = "Add",command = insert).grid(row = 0,column = 1)
Button(window,text = "Delete",command = delete).grid(row = 0,column = 2)
Button(window,text = "Clear",command = clear).grid(row = 0,column = 3)

# Create a listbox widget
l = Listbox(window)
l.grid(row = 1 , column =0,columnspan = 4,sticky = W+E+N+S)

window.mainloop()

Output :

Tkinter listbox example 4 output

That’s wrap for this tutorial.

Leave a Reply