PyQt5基本窗口控件-----QRadioButton

标签: pyqt学习  gui  pyqt

PyQt5学习之路,点击有pyqt5学习笔记
QRadioButton类提供了一组可供选择的按钮和文本标签,为用户提供多选一的操作。

import sys
from PyQt5.QtWidgets import *

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

    def initUI(self):
        self.setWindowTitle('QRdaioButton Demo')
        self.resize(300, 300)

        layout = QHBoxLayout()

        self.button1 = QRadioButton('单选按钮1')
        # 设置选中状态
        self.button1.setChecked(True)

        self.button2 = QRadioButton('单选按钮2')

        self.button1.toggled.connect(self.buttonState)
        self.button2.toggled.connect(self.buttonState)

        layout.addWidget(self.button1)
        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()
    sys.exit(app.exec_())

在这里插入图片描述

版权声明:本文为weixin_44653585原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44653585/article/details/104475896