import PyQt5.QtWidgets as qtw class ComboExample(qtw.QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.combo = qtw.QComboBox(self) self.combo.setGeometry(50, 50, 200, 30) items = ['Item 1', 'Item 2', 'Item 3'] # add items to combo box using a for loop for item in items: self.combo.addItem(item) self.show() if __name__ == '__main__': app = qtw.QApplication([]) comboExample = ComboExample() app.exec_()
import PyQt5.QtWidgets as qtw class ComboExample(qtw.QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.combo = qtw.QComboBox(self) self.combo.setGeometry(50, 50, 200, 30) items = [f"Option {i}" for i in range(1, 6)] # add items to combo box using list comprehension self.combo.addItems(items) self.show() if __name__ == '__main__': app = qtw.QApplication([]) comboExample = ComboExample() app.exec_()In this example, we create a new QComboBox widget and add five items 'Option 1', 'Option 2', 'Option 3', 'Option 4', and 'Option 5' using a list comprehension. These examples use the PyQt5.QtWidgets package library.