# -*- coding:utf-8 -*-
"""
@Time:2022/3/24 0:06
@Author:小柠檬
@File:combo_box.py
"""
from PyQt5 import uic, QtCore
from PyQt5.QtWidgets import QApplication
class ComboBox:
def __init__(self):
# 加载UI界面
self.ui = uic.loadUi("combo_box.ui")
# 1.添加一个下拉选项
self.ui.comboBox.addItem('小柠檬1')
# 2.从列表中添加多个下拉选项
add_list = ['小柠檬2', '小柠檬3', '小柠檬4', '小柠檬5']
self.ui.comboBox.addItems(add_list)
# 3.清空所有下拉选项
# self.ui.comboBox.clear()
# 4.获取下拉选项数目
option_count = self.ui.comboBox.count()
print(option_count)
# 5.获取当前选中的文本
option_text = self.ui.comboBox.currentText()
print(option_text)
# 6.通过索引获取文本
option_text_index = self.ui.comboBox.itemText(2)
print(option_text_index)
# 7.获取当前选中项的索引
option_index = self.ui.comboBox.currentIndex()
print(option_index)
# 8.通过改变指定索引中的文本内容
self.ui.comboBox.setItemText(1, '小柠檬改')
# 9.修改选项发出信号
self.ui.comboBox.currentIndexChanged.connect(self.slot_combo_box)
def slot_combo_box(self):
option_text = self.ui.comboBox.currentText()
print(f'当前选项:{option_text}')
def main():
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) # 和designer设计的窗口比例一致
app = QApplication([])
combo_box = ComboBox()
combo_box.ui.show()
app.exec_()
if __name__ == '__main__':
main()
版权声明:本文为m0_50153253原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。