from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.comboBox = QComboBox(self) self.comboBox.addItems(['Item 1', 'Item 2', 'Item 3']) self.setCentralWidget(self.comboBox) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_()
from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.comboBox = QComboBox(self) self.comboBox.addItems(['Item 1', 'Item 2', 'Item 3']) self.comboBox.currentIndexChanged.connect(self.onComboSelection) self.setCentralWidget(self.comboBox) def onComboSelection(self, index): selected_value = self.comboBox.currentText() print('Selected value:', selected_value) if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec_()In this example, a combo box is created with a list of items and a signal is connected to a method that gets called whenever the selection in the combo box changes. The currentText method is used to retrieve the currently selected value from the combo box. The PyQt5 library provides a collection of widgets and tools to build Python desktop applications with the Qt toolkit.