What is the Tkinter Listbox widget?

Tkinter is a Python GUI module used to create cross-platform desktop applications. It comes with Python's standard library. This module is lightweight and easy to use compared to other GUI modules available in python. In this answer, we'll learn to create and use of Listbox widget in Tkinter.

Syntax

Listbox()

Let us take a look at an example of this.

Example

# Import the Tkinter library
from tkinter import *

# Create an instance of Tkinter window
window=Tk()

# Set the size of the window
window.geometry("300x300")

# Create a label widget
label=Label(window, text="List of fruits: ").pack()

# Create a Listbox widget
listbox = Listbox(window)

# Add elemnets to list
listbox.insert(1,"Apple")
listbox.insert(2,"Orange")
listbox.insert(3,"Banana")

listbox.pack()

window.mainloop()

Note: The above program runs on the Tkinter version 8.6

Explanation

In the above code snippet,

  • Line 5: We create an instance of the Tkinter class, Tk(), and assign it to variable, window.

  • Line 8: We use the geometry method to set the window size as 300x300 px.

  • Line 11: We use the Label() widget to create a label.

  • Line 14: We use the Listbox() widget to create a list and attach it to the window.

  • Line 17, 18 & 19: We use the insert method to insert elements in the listbox. The insert method takes the position as the first parameter and the value as the second parameter.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved