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())
相关推荐
l1258657 分钟前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain
zoujiahui_20189 分钟前
别再只写 np.random.seed() 了:default_rng 与 np.random、random 到底差在哪?
python
Patrick在香港12 分钟前
Claude Agent 进阶:4 种工具调用编排模式,让 0820 那 13 个 endpoint 并行起来
python·ai·ai编程
qq_513728041 小时前
StaleElementReferenceException(陈旧元素引用异常)
python
千里码aicood1 小时前
基于Python的电商数据采集系统(大数据+爬虫)
开发语言·python
战略性的菠萝1 小时前
研究生基础-Python快速入门(终)——面向对象
python
陈皮波比茶1 小时前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
winfredzhang2 小时前
从零构建家庭照片管理系统:Django 模块化单体实战,以及我踩到的 4 个真实坑
python·postgresql·django·sqlite·bootsrap
jyOverQ2 小时前
LangGraph 流式执行详解:stream、astream 与 stream_mode 怎么选?
python·langchain
法哥20122 小时前
用 C++ 控制 CMW100,到底在干什么?
开发语言·c++