Python

Tkinter Listbox

The default GUI toolkit for Python is called Tkinter. The combination of Python with Tkinter makes it quick and simple to develop the GUI apps. An effective object-oriented gateway for the Tk GUI toolkit is provided by Tkinter. It’s simple to build a Gui interface employing the Tkinter. Within this guide, we will show you the use of the Tkinter library to create a Tkinter GUI and add a Listbox widget to it.

Getting started with the Ubuntu 20.04 system, we update our system using some commands in the terminal shell. We launch the terminal application with the Ctrl+Alt+T and add the apt “update” instruction on it. The execution requires the password of the logged-in user and it updates the whole system after the addition of a password.

After the successful installation of python3, you need to install the Python tk utility to use its different widgets in the code. For installation, try out the following command in the terminal query area:

The installation of this utility requires your affirmation before completing itself. Press “y” after being asked the following question:

After the whole installation of this utility, we get the following dialogue box for the “tk” widget on the terminal screen. It has two buttons – one for quitting and one for just a click.

Upon continuously clicking the “Click me!” button, we get the square brackets around the text it contains. The dialogue screen of “Tk” gets bigger in width. Upon tapping on the “Quit
button, the tk dialogue is closed with any issue.

Example 1:

We start our first Python example to display the use of the Tkinter Listbox in the program. For this, we create a new Python file and importe all the related functions of the “Tkinter” library. The GUI object “t” is created in the code using the “Tk()” function. It allows us to create the main GUI window on our screen. The geometry() function is called using the object “t” of Tkinter to create a screen of a specific size.

Now, we create a widget label “l” of text type at the Tkinter GUI screen with some text to label the widget. After this, we create a widget Listbox using the “t” object in the parameters of a “Listbox” function. The insert() function using the Listbox widget is called to add 5 new string values to the Listbox with the specified numbering to create an order.

The label “l” is packed after that using the pack() function. The Listbox is packed. The mainloop() function is called using the “t” object of Tkinter to create a basic loop of events created by a user. This is how a Listbox is utilized in Python via the Tkinter module. The program is now complet and ready for use. Let’s save it in the file and quit it.

#!/usr/bin/python3
from tkinter import *
t = Tk()
t.geometry("200x250")
l = label(t, text = "My favourite colors...")
listbox = Listbox(t)
listbox.insert(1, "White")
listbox.insert(2, "Black")
listbox.insert(3, "Red")
listbox.insert(4, "Blue")
listbox.insert(5, "Yellow")
l.pack()
listbox.pack()
t.mainloop()

After closing the file, we launch the terminal once again and list the main directory contents via the “ls” instruction. It shows that the newly updated Python file is also there. We use python3 to execute the Python file.

Upon execution, the following GUI screen of Tkinter is opened at our screen with the title “tk”. Within the grey-colored area, you can see the labeled text. In the white area, you can see the Listbox items, i.e. items added to the Listbox using the Listbox object. You can close the GUI Tkinter screen using the cross sign given at the right-most corner in red.

Example 2:

Let’s take a look at the use of Listbox along with some other widgets to make it a little interactive. The very same Python script is utilized in the same code file with minor changes at some lines. We add a new code line at line number 12 of this code. We create a button “b” in the Tkinter GUI screen using the “Button” function taking the “Delete” text as a button label and Tkinter object “t”.

The third parameter of the Button() function contains the deletion command for Listbox items using ANCHOR, i.e. selecting an item and deleting it using the button. The label, Listbox, and button are packed. The main event loop is created for the execution of this GUI.

#!/usr/bin/python3
from tkinter import *
t = Tk()
t.geometry("200x250")
l = label(t, text = "My favourite colors...")
listbox = Listbox(t)
listbox.insert(1, "White")
listbox.insert(2, "Black")
listbox.insert(3, "Red")
listbox.insert(4, "Blue")
listbox.insert(5, "Yellow")
b = Button(t, text = "Delete", commnd = lambda listbox=listbox: listbox.delete(ANCHOR))
l.pack()
listbox.pack()
b.pack
t.mainloop()

We execute the same file after saving it.

The output shows the Listbox of 5 items along with a “Delete” button.

We select the “Blue” Listbox item and presse the “Delete” button.

The selected item is deleted from the Listbox.

Now, we update the same code to add an extra functionality. So, on the 3rd line, we update the size of a GUI window. In the 5th line of code, we add a definition for the “showSelected()” function. This function calls the config() function using the following object to get the selected item text from the Listbox “Lbx”. On line 15, the button calls the showSelected() function in its command parameter.

#!/usr/bin/python3
from tkinter import *
t = Tk()
t.geometry("400x300")
def showSelected():
   show.config(text=Lbx.get(ANCHOR))
l = label(t, text = "My favourite colors...")
Lbx = Listbox(t)
Lbx.pack()
Lbx.insert(1, "White")
Lbx.insert(2, "Black")
Lbx.insert(3, "Red")
Lbx.insert(4, "Blue")
Lbx.insert(5, "Yellow")
Button(t, text = 'Show Selected", commnd=showSelected).pack()
show = Label(t)
show.pack
t.mainloop()

We execute the updated code.

The following screen of Listbox with the “Show Selected” button is created.

We selecte the Listbox item “White” and tap the “Show Selected” button. The “White” text is displayed on the GUI screen after the button.

Conclusion

That’s all about the usage of the Tkinter module in Python. We added a total of 2 simple Python examples to see how we can use the Listbox in the Python code via the Tkinter library. We have seen how the different widgets can be used to make Tkinter GUI more interactive, especially the creation of Listbox and related buttons.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content