【QTdesigner】课时38.单选按钮控件(QRadioButton)【pyqt5+QTdesigner模式】

** >>> 点击进入:pyqt5专栏<<<**

老师原课件下载地址:
有积分的朋友,支持下我,打赏也OK。
不下载也可以,我每节课会吧代码贴出来


视频链接

P37课时38.单选按钮控件(QRadioButton)


课程重点

多选一的按钮
在这里插入图片描述


代码位置

在这里插入图片描述


完整代码

'''

单选按钮控件(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()
    sys.exit(app.exec_())

遇到的问题


改写效果展示

在这里插入图片描述


改写注意点

在这里插入图片描述
在这里插入图片描述


改写代码(QTdesigner模式)

# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.csdn.net/zzx188891020
@E-mail: 188891020@qq.com
@File: class38.py
@CreateTime: 2020/5/15 15:12
'''

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic


class my_form():
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi("../ui_package/class38.ui")
        self.ui.radioButton.toggled.connect(self.buttonState)
        self.ui.radioButton_2.toggled.connect(self.buttonState)

    def buttonState(self):
        radioButton = self.ui.sender()

        if radioButton.isChecked() == True:
            print('<' + radioButton.text() + '> 被选中')
        else:
            print('<' + radioButton.text() + '> 被取消选中状态')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = my_form()
    main.ui.show()
    sys.exit(app.exec_())

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值