PyQt5 Tutorial – Create List Box in Python

In this PyQt5 Tutorial article we want to learn that How to Create List Box in Python 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 that. 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 our 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 have defined 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 at name of 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 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

PyQt5 Tutorial - Create List Box in Python
PyQt5 Tutorial – Create List Box in Python

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×