A combobox can be created with the QComboBox class. Items are added using the class method addItem, followed by a string parameter.
Sometimes its called a listbox, as it shows a list of items a user can select from. If you want a user to choose from a set of items, the listbox is the best option.
Related course:
Combobox example
The code below creates a combobox using the class QComboBox.
0 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 26 27 28 29 30 31 |
import sys from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QComboBox from PyQt5.QtCore import QSize, QRect class ExampleWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.setMinimumSize(QSize(640, 140)) self.setWindowTitle("Combobox example") centralWidget = QWidget(self) self.setCentralWidget(centralWidget) # Create combobox and add items. self.comboBox = QComboBox(centralWidget) self.comboBox.setGeometry(QRect(40, 40, 491, 31)) self.comboBox.setObjectName(("comboBox")) self.comboBox.addItem("PyQt") self.comboBox.addItem("Qt") self.comboBox.addItem("Python") self.comboBox.addItem("Example") if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) mainWin = ExampleWindow() mainWin.show() sys.exit( app.exec_() ) |