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.

Practice now: Test your Python skills with interactive challenges
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:
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:
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:
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.
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_())
Practice now: Test your Python skills with interactive challenges
Practice now: Test your Python skills with interactive challenges