pyqt5-下拉框

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QComboBox


class ComboBoxExample(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()

        # 创建标签
        label = QLabel("请选择一个选项:")
        layout.addWidget(label)

        # 创建下拉框
        self.combo_box = QComboBox()
        self.combo_box.addItem("选项1")
        self.combo_box.addItem("选项2")
        self.combo_box.addItem("选项3")
        self.combo_box.addItem("选项4")
        self.combo_box.addItem("选项5")
        layout.addWidget(self.combo_box)

        # 为下拉框绑定事件处理函数
        self.combo_box.currentIndexChanged.connect(self.on_combobox_changed)

        self.setLayout(layout)
        self.setWindowTitle("ComboBox Example")

    def on_combobox_changed(self):
        # 获取当前选中的项的索引和文本
        index = self.combo_box.currentIndex()
        text = self.combo_box.currentText()
        print("选中项索引:", index)
        print("选中项文本:", text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ComboBoxExample()
    window.show()
    sys.exit(app.exec_())

pyqt5-下拉框_第1张图片

 

你可能感兴趣的:(PyQt5,qt,python)