How to Create List Box in PyQt5

In this PyQt5 article we want to learn that How to Create List Box in PyQt5, PyQt5 list box is also known as a QListWidget, it is a widget that provides a list of items that the user can select from. it is commonly used widget in GUI applications for displaying and selecting multiple items. QListWidget class in PyQt5 provides several methods to manipulate the list box, including adding and removing items, selecting items and retrieving selected items. it also emits different signals to indicate user interactions, such as when an item is clicked or selected.



This is the complete code for this article



To begin, we need to import the necessary modules.



After that we are going to create a class that inherits from QWidget, and it will serve as our main application window. we name it ListboxApp.

In the above code, we define the ListboxApp class and initialize it by calling the parent __init__() method. after that we define the init_ui() method, which sets the window title and geometry.



Inside the init_ui() method, let’s add a list box widget using QListWidget. we also creates a label widget that will display the selected item from the list box.

In the above code, we have created a QListWidget named list_widget and set its position and size using setGeometry(). after that we add items to the list box using addItems(). also we connects the itemClicked signal of the list_widget to a custom slot method item_clicked().



Let’s define the item_clicked() method, which will receive the signal emitted when an item in the list box is clicked. Inside this method, we will retrieve the selected item text and update the label text.




Run the complete code and this will be the result

How to Create List Box in PyQt5
How to Create List Box in PyQt5



Learn More

Leave a Comment