python--pyQt 单选按钮控件 -QRadioButton

控件用来创建单选按钮,允许在一组选项中选择其中一个选项。

用法

text() 获取按钮文本

setText() 设置按钮文本

setCheckable() 设置按钮被选中,设置为True则选中,设置为False则取消选中

isChecked() 获取按钮是否被选中,选中返回True,未选中返回False

常用信号

toggled 单选按钮状态改变时发出信号

例子

bash 复制代码
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QRadioButton, QHBoxLayout


class QRadioButtonDemo(QWidget):
    def __init__(self):
        super(QRadioButtonDemo, self).__init__()
        self.init_ui()

    def init_ui(self):
        h_layout = QHBoxLayout(self)

        radio_btn1 = QRadioButton('选项1')
        radio_btn1.setCheckable(True)  # 默认选中
        radio_btn1.toggled.connect(self.radio_status)  # 绑定状态变化信号

        radio_btn2 = QRadioButton('选项2')
        radio_btn2.toggled.connect(self.radio_status)

        h_layout.addWidget(radio_btn1)
        h_layout.addWidget(radio_btn2)

    def radio_status(self):
        res = self.sender()  # 获取选中的控件对象
        print(res)
        if res.isChecked():
            print(f'{res.text()}被选中')
        else:
            print(f'{res.text()}被取消')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QRadioButtonDemo()
    w.show()
    sys.exit(app.exec())
相关推荐
常利兵3 分钟前
一文搞懂双Token、SSO与第三方权限打通,附实战代码
python·gitee·kotlin
认真的小羽❅4 分钟前
JavaScript完全指南:从入门到精通
开发语言·javascript·ecmascript
xuhaoyu_cpp_java8 分钟前
JAVA线程安全类
java·开发语言
香水5只用六神13 分钟前
【TIM】基本定时器定时实验(2)
c语言·开发语言·stm32·单片机·嵌入式硬件·mcu·学习
BatyTao17 分钟前
Python从零起步-数据容器
开发语言·python
承渊政道17 分钟前
C++学习之旅【C++伸展树介绍以及红黑树的实现】
开发语言·c++·笔记·b树·学习·visual studio
郭涤生21 分钟前
C++中设置函数与回调函数设值的性能差异及示例
开发语言·c++
m0_6356474824 分钟前
Qt开发与MySQL数据库教程(二)——MySQL常用命令以及示例
java·开发语言·数据库·mysql
fie888934 分钟前
Spinal码MATLAB实现(采用One-at-a-Time哈希函数)
开发语言·matlab·哈希算法
ZHOUPUYU1 小时前
PHP 8.6的底层革命。那些看不见的优化,才是真正的惊喜
开发语言·后端·php