https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QComboBox.html
| 函数 | 作用 |
|---|---|
| addItem | 添加一个下拉选项 |
| addItems | 添加多个下拉选项 |
| currentIndex | 返回当前的下拉选项索引 |
| currentText | 返回当前下拉选项文本 |
| count | 返回下拉列表框中全部选项的个数 |
| removeItem | 删除选项 |
| clear | 清空所有选项 |
| insertItem | 将选项添加到指定的index位置 |
| setCurrentIndex | 显示指定index位置的选项 |
| 信号 | 作用 |
|---|---|
| currentIndexChanged | 当下拉列表选项改变的时候触发 |
ComboBox添加选项有两种方式,分别是程序设置和界面设置
self.ui.comboBox.addItem("苹果") # 添加单个选项
self.ui.comboBox.addItems(["葡萄","香蕉","西瓜"]) # 添加多个选项
打开designer软件,双击ComboBox控件,通过"+ -"符号添加和删除选项。

print(self.ui.comboBox.currentIndex()) # 打印出当前的选项
print(self.ui.comboBox.currentText()) # 打印出当前选项的文本
print(self.ui.comboBox.count()) # 打印出下拉列表框的选项个数
def additem_func(self):
self.ui.comboBox.insertItem(0,str(datetime.now())) # 添加单个选项,并设置在index为0的位置
self.ui.comboBox.setCurrentIndex(0) # 显示index为0的选项
def deleteitem_func(self):
self.ui.comboBox.removeItem(self.ui.comboBox.currentIndex()) # 移除选项
def combobox_index_change_func(self,index):
print(self.ui.comboBox.currentText(),index) # 打印出选中的选项文本
<ui version="4.0">
<class>MainWindowclass>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0x>
<y>0y>
<width>386width>
<height>283height>
rect>
property>
<property name="windowTitle">
<string>MainWindowstring>
property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>50number>
property>
<item>
<widget class="QComboBox" name="comboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>98horstretch>
<verstretch>0verstretch>
sizepolicy>
property>
<property name="maximumSize">
<size>
<width>250width>
<height>20height>
size>
property>
<property name="sizeIncrement">
<size>
<width>98width>
<height>100height>
size>
property>
<property name="baseSize">
<size>
<width>100width>
<height>0height>
size>
property>
widget>
item>
layout>
item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="deleteitem_btn">
<property name="maximumSize">
<size>
<width>100width>
<height>16777215height>
size>
property>
<property name="text">
<string>删除选项string>
property>
widget>
item>
<item>
<widget class="QPushButton" name="additem_btn">
<property name="maximumSize">
<size>
<width>100width>
<height>16777215height>
size>
property>
<property name="text">
<string>增加选项string>
property>
widget>
item>
layout>
item>
layout>
widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0x>
<y>0y>
<width>386width>
<height>22height>
rect>
property>
widget>
<widget class="QStatusBar" name="statusbar"/>
widget>
<resources/>
<connections/>
ui>
# Import Qt libraries
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile
# Import UI developed in Qt Creator
from combobox_ui import Ui_MainWindow # 导入界面
# Import PseudoSensor
# Import system tools and datetime
import sys
import statistics
import time
from datetime import datetime
# Create and start the Qt application
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# 设置界面为用户设计的界面
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.comboBox.addItem("苹果") # 添加单个选项
self.ui.comboBox.addItems(["葡萄","香蕉","西瓜"]) # 添加多个选项
print(self.ui.comboBox.currentIndex()) # 打印出当前的选项
print(self.ui.comboBox.currentText()) # 打印出当前选项的文本
print(self.ui.comboBox.count()) # 打印出下拉列表框的选项个数
self.ui.comboBox.currentIndexChanged.connect(self.combobox_index_change_func)
self.ui.additem_btn.clicked.connect(self.additem_func)
self.ui.deleteitem_btn.clicked.connect(self.deleteitem_func)
def additem_func(self):
self.ui.comboBox.insertItem(0,str(datetime.now())) # 添加单个选项,并设置在index为0的位置
self.ui.comboBox.setCurrentIndex(0) # 显示index为0的选项
def deleteitem_func(self):
self.ui.comboBox.removeItem(self.ui.comboBox.currentIndex()) # 移除选项
def combobox_index_change_func(self,index):
print(self.ui.comboBox.currentText(),index) # 打印出选中的选项文本
def closeAndExit(self):
sys.exit()
if __name__ == "__main__":
app = QApplication(sys.argv) # 初始化QApplication
# 初始化界面并显示界面
window = MainWindow()
window.show()
window.setFixedSize(window.width(), window.height())
sys.exit(app.exec())
