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.
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 | self.listwidget.insertItem(0, "Red") |
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 | from PyQt5.QtWidgets import * |
If you are new to Python PyQt, then I highly recommend this book.