python--pyQt5 pyside6 中 下拉列表框(QComboBox)
warning:
这篇文章距离上次修改已过207天,其中的内容可能已经有所变动。
在 PyQt5 或 PySide6 中创建下拉列表框(QComboBox)的基本示例代码如下:
使用 PyQt5:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
# 创建QComboBox对象
combo_box = QComboBox()
# 添加条目
combo_box.addItem("选项1")
combo_box.addItem("选项2")
combo_box.addItem("选项3")
# 当选项发生变化时,打印当前选中的文本
combo_box.currentTextChanged.connect(lambda text: print(text))
layout.addWidget(combo_box)
window.setLayout(layout)
window.show()
app.exec_()
使用 PySide6:
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
# 创建QComboBox对象
combo_box = QComboBox()
# 添加条目
combo_box.addItem("选项1")
combo_box.addItem("选项2")
combo_box.addItem("选项3")
# 当选项发生变化时,打印当前选中的文本
combo_box.currentTextChanged.connect(lambda text: print(text))
layout.addWidget(combo_box)
window.setLayout(layout)
window.show()
app.exec()
这两段代码都创建了一个包含下拉列表框的窗口,下拉列表框中有三个选项,并且当选项变化时,会在控制台打印出当前选中的文本。在实际应用中,你可以根据需要添加更多的选项或者为 QComboBox 对象添加其他属性和方法。
评论已关闭