Python 3 – Tkinter Listbox
Python 3 is a very popular programming language, often used for creating desktop applications. One of its powerful packages is Tkinter, a library for building graphical user interfaces (GUIs).
In this article, we will be discussing Listbox, one of the widgets in the Tkinter package. We will explore what it is and how it can be used to enhance GUI development.
What is Tkinter Listbox?
Tkinter Listbox is a widget that displays a list of items from which a user can select one or more items. The list is displayed in a scrollable box, and the user can navigate the list using the mouse or keyboard.
Creating a Tkinter Listbox
Creating a listbox in Tkinter is very easy. Consider the following code that creates an empty listbox:
from tkinter import *
root = Tk()
listbox = Listbox(root)
listbox.pack()
root.mainloop()
Here, we begin by importing the Tkinter module and then create a top-level Tkinter window using the Tk()
function. In this example, we create an empty listbox using the Listbox
class and then pack it inside the Tkinter window using the pack()
method. Finally, we enter the Tkinter event loop using the mainloop()
method.
If we run this code, we will see an empty listbox displayed in the window.
Adding Items to the Listbox
To add items to the listbox, we use the insert()
method. Here is an example of how to add three items to the listbox:
from tkinter import *
root = Tk()
listbox = Listbox(root)
listbox.pack()
listbox.insert(1, "Item 1")
listbox.insert(2, "Item 2")
listbox.insert(3, "Item 3")
root.mainloop()
Here, we use the insert()
method to add three items to the listbox. The first argument is the index at which the item should be inserted, and the second argument is the item itself. In this example, we add three items at positions 1, 2, and 3.
The items will be displayed in the order in which they were inserted.
Configuring the Listbox
We can configure the appearance of the listbox by setting various properties. Some of the properties that we can set include:
bg
: the background color of the listboxfg
: the font color of the listboxfont
: the font used to display the items in the listboxheight
: the height of the listbox (in number of items displayed)width
: the width of the listbox (in number of characters)selectmode
: the selection mode of the listbox (single, multiple, or extended)
For instance, to set the background color of the listbox to red and the font to Arial, we can modify the code as follows:
from tkinter import *
root = Tk()
listbox = Listbox(root, bg='red', font=('Arial', 12))
listbox.pack()
listbox.insert(1, "Item 1")
listbox.insert(2, "Item 2")
listbox.insert(3, "Item 3")
root.mainloop()
Here, we set the background color to red using the bg
property, and the font to Arial using the font
property.
Getting Selected Items
To find out which items are selected in the listbox, we can use the curselection()
method. For example:
from tkinter import *
def get_selected_items():
selection = listbox.curselection()
for item in selection:
print(listbox.get(item))
root = Tk()
listbox = Listbox(root)
listbox.pack()
listbox.insert(1, "Item 1")
listbox.insert(2, "Item 2")
listbox.insert(3, "Item 3")
button = Button(root, text="Get Selected Items", command=get_selected_items)
button.pack()
root.mainloop()
Here, we define a function get_selected_items()
that gets called when the “Get Selected Items” button is clicked. The function uses the curselection()
method to get the indices of the selected items in the listbox, and then uses the get()
method to retrieve the text of each selected item.
Conclusion
Tkinter Listbox is a powerful widget that can be used to display lists of items in a user-friendly way. It is easy to create and customize, and provides a lot of functionality out of the box. We hope that this article has given you a good idea of how Listbox can be used to enhance GUI development in Python 3.