from PyQt5.QtWidgets import QApplication, QComboBox app = QApplication([]) combo_box = QComboBox() combo_box.addItems(["Option 1", "Option 2", "Option 3"]) combo_box.setCurrentIndex(1) # set the default selection as Option 2 selected_item_text = combo_box.itemText(combo_box.currentIndex()) print(selected_item_text)
Option 2
from PyQt5.QtWidgets import QApplication, QComboBox, QLabel, QVBoxLayout, QWidget app = QApplication([]) window = QWidget() layout = QVBoxLayout(window) combo_box = QComboBox() combo_box.addItems(["Option 1", "Option 2", "Option 3"]) label = QLabel(combo_box.itemText(combo_box.currentIndex())) combo_box.currentIndexChanged.connect(lambda: label.setText(combo_box.itemText(combo_box.currentIndex()))) layout.addWidget(combo_box) layout.addWidget(label) window.show() app.exec()This example uses a QVBoxLayout to arrange a QComboBox and a QLabel widget. The QLabel is set to display the selected item's text initially, and it is updated when the combo box selection is changed. The currentIndexChanged signal of the QComboBox is connected to a lambda function that updates the label's text when a new item is selected. Package library: PyQt5.QtWidgets