Python - GUI Programming - Listbox Widget
The Listbox widget in Tkinter is used to display a list of items from which the user can select one or more items. It is similar to the Combobox widget, but instead of a drop-down menu, it displays a list of items that can be scrolled up and down.
The Listbox widget can be used to display a list of options for the user to choose from, or it can be used to display a list of items that the user has selected. In this tutorial, we will explore the different options available in the Listbox widget and how to use them in Python.
Creating a Listbox widget:
To create a Listbox widget in Tkinter, we first need to import the Tkinter module and then create an instance of the Tk class. After that, we can create a Listbox widget by calling the Listbox() method on the root window.
Here is an example code to create a simple Listbox widget:
import tkinter as tk
root = tk.Tk()
listbox = tk.Listbox(root)
listbox.pack()
root.mainloop()
In the above code, we created an instance of the Tk class and then created a Listbox widget using the Listbox() method. We then used the pack() method to pack the Listbox widget into the root window.
Adding items to the Listbox:
Once we have created a Listbox widget, we can add items to it using the insert() method. The insert() method takes two arguments: the index at which to insert the item and the item itself.
Here is an example code to add items to the Listbox widget:
import tkinter as tk
root = tk.Tk()
listbox = tk.Listbox(root)
listbox.pack()
listbox.insert(0, "Item 1")
listbox.insert(1, "Item 2")
listbox.insert(2, "Item 3")
root.mainloop()
In the above code, we added three items to the Listbox widget using the insert() method. The first argument to the insert() method specifies the index at which to insert the item. In this example, we inserted the items at index 0, 1, and 2.
Deleting items from the Listbox:
We can delete items from the Listbox widget using the delete() method. The delete() method takes two arguments: the starting index of the items to delete and the ending index of the items to delete.
Here is an example code to delete items from the Listbox widget:
import tkinter as tk
root = tk.Tk()
listbox = tk.Listbox(root)
listbox.pack()
listbox.insert(0, "Item 1")
listbox.insert(1, "Item 2")
listbox.insert(2, "Item 3")
listbox.delete(1)
root.mainloop()
In the above code, we added three items to the Listbox widget using the insert() method. We then deleted the item at index 1 using the delete() method.
We can configure the Listbox widget to allow single selection or multiple selections using the selectmode parameter. Here is an example of how to configure the Listbox widget for single selection:
import tkinter as tk
root = tk.Tk()
root.title("Listbox Widget - Single Selection Example")
listbox = tk.Listbox(root, selectmode=tk.SINGLE)
listbox.pack()
# Add items to the listbox
listbox.insert(tk.END, "Option 1")
listbox.insert(tk.END, "Option 2")
listbox.insert(tk.END, "Option 3")
root.mainloop()
In this example, we have set the selectmode parameter to tk.SINGLE, which allows only one item to be selected at a time in the Listbox widget.
We can also configure the Listbox widget for multiple selections using the selectmode parameter. Here is an example of how to configure the Listbox widget for multiple selections:
import tkinter as tk
root = tk.Tk()
root.title("Listbox Widget - Multiple Selection Example")
listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
listbox.pack()
# Add items to the listbox
listbox.insert(tk.END, "Option 1")
listbox.insert(tk.END, "Option 2")
listbox.insert(tk.END, "Option 3")
root.mainloop()
In this example, we have set the selectmode parameter to tk.MULTIPLE, which allows multiple items to be selected at a time in the Listbox widget.