A Listbox (QListWidget) presents several options. You can click on an item. An example of a listbox would be a song playlist. Unlike a combobox it shows all possible options.

The screenshot below shows a PyQt list box in a window.

pyqt listbox

Related Course: Create GUI Apps with Python PyQt5

PyQt Listbox example

QListWidget

A listbox widget is created with QListWidget(), it creates an item-based list widget. The QListWidget provides a list view similar to the one supplied by QListView, but with a classic item-based interface. With a single line a listwidget is added to a window:

1
self.listwidget = QListWidget()

An item is then added with the method .insertItem(). For example: self.listwidget.insertItem(0, "Red") where the first parameter is the index. You can add many items that way:

1
2
3
4
self.listwidget.insertItem(0, "Red")
self.listwidget.insertItem(1, "Orange")
self.listwidget.insertItem(2, "Blue")
...

Now if you click on any of the items it won’t do anything. So you need to event the slot to a function call. You can do that with the line:

1
self.listwidget.clicked.connect(self.clicked)

Where clicked() is a slot or method that is called (a callback method).
The click event is added with the method clicked, self.listwidget.clicked.connect(self.clicked)

This example below shows how to use the list widget (QListWidget) in PyQt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)
self.listwidget = QListWidget()
self.listwidget.insertItem(0, "Red")
self.listwidget.insertItem(1, "Orange")
self.listwidget.insertItem(2, "Blue")
self.listwidget.insertItem(3, "White")
self.listwidget.insertItem(4, "Green")
self.listwidget.clicked.connect(self.clicked)
layout.addWidget(self.listwidget)

def clicked(self, qmodelindex):
item = self.listwidget.currentItem()
print(item.text())

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

If you are new to Python PyQt, then I highly recommend this book.

Download Examples