Pysied6 ComboBox


Pyside6的ComboBox下拉列表框,可以给用户提供一系列的选项,下面就来简单了解一下Pysied6 ComboBox的使用。更多关于ComboBox的使用可以参考下面的文档。

https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QComboBox.html

Pysied6 ComboBox

ComboBox常用函数

函数作用
addItem添加一个下拉选项
addItems添加多个下拉选项
currentIndex返回当前的下拉选项索引
currentText返回当前下拉选项文本
count返回下拉列表框中全部选项的个数
removeItem删除选项
clear清空所有选项
insertItem将选项添加到指定的index位置
setCurrentIndex显示指定index位置的选项

ComboBox常用信号

信号作用
currentIndexChanged当下拉列表选项改变的时候触发

例程

ComboBox添加选项

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) # 打印出选中的选项文本

完整程序

界面程序
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>386</width>
    <height>283</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout_2">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
      <property name="leftMargin">
       <number>50</number>
      </property>
      <item>
       <widget class="QComboBox" name="comboBox">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
          <horstretch>98</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>250</width>
          <height>20</height>
         </size>
        </property>
        <property name="sizeIncrement">
         <size>
          <width>98</width>
          <height>100</height>
         </size>
        </property>
        <property name="baseSize">
         <size>
          <width>100</width>
          <height>0</height>
         </size>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QPushButton" name="deleteitem_btn">
        <property name="maximumSize">
         <size>
          <width>100</width>
          <height>16777215</height>
         </size>
        </property>
        <property name="text">
         <string>删除选项</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="additem_btn">
        <property name="maximumSize">
         <size>
          <width>100</width>
          <height>16777215</height>
         </size>
        </property>
        <property name="text">
         <string>增加选项</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>386</width>
     <height>22</height>
    </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())

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VB6中,可以通过自绘的下拉式列表控件ComboBox实现在输入框前显示图标的功能。这个控件的功能与VB6自带的ComboBox类似,只是在外观界面设计上做了美化。作者在美化方面做了一些改进,比如在输入框前显示一个图标,添加了光标热追踪、选中的样式、滚动条平面化和渐变化等效果。 如果想要在VB6的ComboBox中显示图片,可以参考以下步骤: 1. 首先,在窗体中添加一个ComboBox和一个ImageList控件。 2. 设置ComboBox的DrawMode属性为OwnerDrawFixed,这样能够自定义绘制ComboBox的外观。 3. 在ImageList中添加需要显示的图片。 通过以上步骤,就可以在VB6的ComboBox中显示图标了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vb combobox.ocx](https://download.csdn.net/download/ty5858/87925426)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [VB把图片添加到ComboBox中显示.rar](https://download.csdn.net/download/weixin_39841856/11320811)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [在combobox控件中添加图标](https://download.csdn.net/download/xixi18haha/2633557)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值