六、PyQt5实现Python界面设计_选择控件(QRadioButton、QCheckBox、QComboBox)

目录

一、QRadioButton

(1)QRadioButton为单选框控件,即为多选一的情况下使用,具体内容请观看官方文档

(2)实例演示

二、QCheckBox

(1)QCheckBox小部件提供一个带有文本标签的复选框。

(2)主要函数

(3)主要信号

(4)官方文档

(5)实例演示

三、QComboBox

(1)QComboBox小部件是组合的按钮和弹出列表。

(2)主要函数

(3)槽

(4)信号

(5)函数众多,详细请观看官方手册

(6)实例


一、QRadioButton

(1)QRadioButton为单选框控件,即为多选一的情况下使用,具体内容请观看官方文档

QRadioButton — Qt for Python

(2)实例演示

'''
单选按钮控件(QRadioButton)
'''

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class QRadioButtonDemo(QWidget):
    def __init__(self):
        super(QRadioButtonDemo,self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle('QRadioButton')
        layout = QHBoxLayout()
        self.button1 = QRadioButton('单选按钮1')
        self.button1.setChecked(True)#设置单选按钮为选中状态

        self.button1.toggled.connect(self.buttonState)
        layout.addWidget(self.button1)

        self.button2 = QRadioButton('单选按钮2')
        self.button2.toggled.connect(self.buttonState)
        layout.addWidget(self.button2)
        self.setLayout(layout)


    def buttonState(self):
        radioButton = self.sender()
        if radioButton.isChecked() == True:#判断单选按钮是否被选中
            print('<' + radioButton.text() + '> 被选中')
        else:
            print('<' + radioButton.text() + '> 被取消选中状态')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QRadioButtonDemo()
    main.show()
    app.exec_()
    del app

二、QCheckBox

(1)QCheckBox小部件提供一个带有文本标签的复选框。

(2)主要函数

checkState():返回复选框的检查状态。如果不需要三态支持,还可以使用isChecked(),它返回一个布尔值。

initStyleOption(option):使用此QCheckBox中的值初始化option。对于需要QStyleOptionButton,但又不想自己填写所有信息的子类,此方法很有用。

isTristate():此属性保存该复选框是否为三态复选框。默认值为false,即该复选框只有两个状态。

setCheckState(state):将复选框的检查状态设置为state。如果不需要三态支持,则也可以使用setChecked(),它需要一个布尔值。

setTristate([y=true]):此属性保存该复选框是否为三态复选框。默认值为false,即该复选框只有两个状态。

(3)主要信号

stateChanged(arg__1)

(4)官方文档

QCheckBox — Qt for Python

(5)实例演示

'''
复选框控件(QCheckBox)
3种状态
未选中:0
半选中:1
选中:2
'''

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt

class QCheckBoxDemo(QWidget):
    def __init__(self):
        super(QCheckBoxDemo,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('复选框控件演示')

        layout = QHBoxLayout()

        self.checkBox1 = QCheckBox('复选框控件1')
        self.checkBox1.setChecked(True)#设置复选框为选中的状态
        self.checkBox1.stateChanged.connect(lambda:self.checkboxState(self.checkBox1))
        layout.addWidget(self.checkBox1)

        self.checkBox2 = QCheckBox('复选框控件2')
        self.checkBox2.stateChanged.connect(lambda:self.checkboxState(self.checkBox2))
        layout.addWidget(self.checkBox2)

        self.checkBox3 = QCheckBox('半选中')
        self.checkBox3.stateChanged.connect(lambda:self.checkboxState(self.checkBox3))
        self.checkBox3.setTristate(True)#True为三种状态,False为两种状态
        #设置为半选中Qt.Checked为选中,Qt.Unckecked为未选中,Qt.PartiallyChecked为半选中
        self.checkBox3.setCheckState(Qt.PartiallyChecked)
        layout.addWidget(self.checkBox3)

        self.setLayout(layout)

    def checkboxState(self,cb):
        check1Status = self.checkBox1.text() + ', isChecked=' + str(self.checkBox1.isChecked()) + ',checkState=' + str(self.checkBox1.checkState()) + '\n'
        check2Status = self.checkBox2.text() + ', isChecked=' + str(self.checkBox2.isChecked()) + ',checkState=' + str(self.checkBox2.checkState()) + '\n'
        check3Status = self.checkBox3.text() + ', isChecked=' + str(self.checkBox3.isChecked()) + ',checkState=' + str(self.checkBox3.checkState()) + '\n'
        print(check1Status + check2Status + check3Status)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QCheckBoxDemo()
    main.show()
    app.exec_()
    del app

三、QComboBox

(1)QComboBox小部件是组合的按钮和弹出列表。

(2)主要函数

addItem(icon,text[,userData=None]):使用给定的图标和文本将一个项目添加到组合框中,并包含指定的userData(存储在UserRole中)。该项目将添加到现有项目列表中。

addItem(text[,userData=None]):使用给定的text将一个项添加到组合框中,并包含指定的userData(存储在UserRole中)。该项目将添加到现有项目列表中。

addItems(texts):将给定文本中的每个字符串添加到组合框。每个项目依次附加到现有项目列表中。

(3)槽

clear():清除组合框,删除所有项目。注:如果在组合框中设置了外部模型,则在调用此函数时仍将清除此模型。

clearEditText():清除组合框中用于编辑的行编辑的内容。

setCurrentIndex(index):此属性保存组合框中当前项目的索引。插入或删除项目时,当前索引可以更改。默认情况下,对于空的组合框或未设置当前项目的组合框,此属性具有一个值-1。

(4)信号

activated(arg__1)

currentIndexChanged(arg__1)

editTextChanged(arg__1)

(5)函数众多,详细请观看官方手册

QComboBox — Qt for Python

(6)实例

'''
下拉列表控件(QComboBox)
1. 如果将列表项添加到QComboBox控件中
2. 如何获取选中的列表项
'''

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class QComboBoxDemo(QWidget):
    def __init__(self):
        super(QComboBoxDemo,self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('下拉列表控件演示')
        self.resize(300,100)

        layout = QVBoxLayout()
        
        self.label = QLabel('请选择编程语言')
        self.button1=QPushButton('删除第一个元素')
        
        self.cb = QComboBox()#实例化QComboBox控件对象
        self.cb.addItem('C++')#新增选择项
        self.cb.addItem(QIcon("./picture/QT.jpeg"),'Python')#新增带有图标的选择项
        self.cb.addItems(['Java','C#','Ruby','VB','Html'])

        self.cb.currentIndexChanged.connect(self.selectionChange)
        self.button1.clicked.connect(lambda:self.deleteFirtItems(self.cb))
        
        layout.addWidget(self.label)
        layout.addWidget(self.cb)
        layout.addWidget(self.button1)

        self.setLayout(layout)

    def deleteFirtItems(self,combox):
        combox.removeItem(0)#删除第0项
    
    def selectionChange(self,i):
        self.label.setText(self.cb.currentText())
        self.label.adjustSize()
        
        for count in range(self.cb.count()):
            print('item' + str(count) + '=' + self.cb.itemText(count))

        print('current index',i,'selection changed', self.cb.currentText())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QComboBoxDemo()
    main.show()
    app.exec_()
    del app

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值