[Pyqt5.5 for Python3.4.3 学习笔记]-->QComboBox组合下拉框使用方法

来源:赵克立博客 分类: Python 标签:PythonPyQt5发布时间:2016-06-02 21:29:26最后更新:2016-06-06 22:38:34浏览:16717
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2016-06-06 22:38:34
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
# -*- coding: utf-8 -*-
"""第一个程序"""
#from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QColor
import sys
class myDialog(QDialog):
    """docstring for myDialog"""
    def __init__(self, arg=None):
        super(myDialog, self).__init__(arg)
        self.setWindowTitle("first window")
        self.resize(400,100);
        addbtn=QPushButton('添加')
        delbtn=QPushButton('清空')
        conLayout = QHBoxLayout()
        self.sexComboBox=QComboBox()
        self.sexComboBox.insertItem(0,self.tr("男"))
        self.sexComboBox.insertItem(1,self.tr("女"))
        conLayout.addWidget(self.sexComboBox)
        conLayout.addWidget(addbtn)
        conLayout.addWidget(delbtn)
        self.setLayout(conLayout)
        self.sexComboBox.currentIndexChanged.connect(self.comboxchange)
        addbtn.clicked.connect(self.additem)
        delbtn.clicked.connect(self.clearComboBox)
    def clearComboBox(self):
        #清空组合框
        self.sexComboBox.clear()
    def additem(self):
        #添加文本
        self.sexComboBox.addItem('测试数据')
    def comboxchange(self):
        QMessageBox.warning(self,"警告",str(self.sexComboBox.currentIndex())+self.tr(':')+self.sexComboBox.currentText(),QMessageBox.Yes)
app = QApplication(sys.argv)
#全局设置QPushButton的背景样式
dlg = myDialog()
dlg.show()
dlg.exec_()
app.exit()

效果图如下

QQ截图20160602212820.png

取当前组合框的索引和文本

sexComboBox.currentIndex()
sexComboBox.currentText()



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python