You can have a listbox, selectbox or combobox with QComboBox. To use this widget, import QComboBox from PyQt5.QtWidgets.
Typically you’d see this widget when a user needs to choose from a select number of items, like country or contract.
Related Course: Create GUI Apps with Python PyQt5
QComboBox
Create a listbox
You can create a list box with these lines:
1 | combo = QComboBox(self) |
The method addItem adds an option to the list box. You can call that as many times as you need to with different options.
To connect a listbox change with a method, you can use this:
1 | combo.activated[str].connect(self.onChanged) |
Example
The code below adds a combobox to a window. Once you select one of the options presented in the combo box, the label values changes.
1 | import sys |
If you are new to Python PyQt, then I highly recommend this book.