Pyside6 QRadioButton

news2024/10/1 15:18:49

Pyside6 QRadioBox

  • QRadioButton使用
    • QRadioButton分组
    • QRadioButton设置文本
      • 代码设置
      • 界面设置
    • QRadioButton禁用和启用
      • 代码设置
      • 界面设置
    • QRadioButton设置默认值
      • 代码设置
      • 界面设置
    • 读取QRadioButton状态
    • QRadioButton样式设计
      • 代码设置
      • 界面设置
    • 完整程序
      • 界面程序
      • 主程序

QRadioButton单选框,QRadioButton是一个可以切换选中和未选中的控件,它的作用跟QCheckBox复选框一样,但是复选框可以实现多选多的功能,而单选框只能实现多选一的功能。
QRadioButton都是默认开启了自动互斥功能,意味着属于同一个父部件的所有单选框都是相互排斥,也就是只能有一个单选框被选中。在实际应用中通常需要对单选框进行分组,以实现不同的功能。关于QRadioButton的更多资料可以参考下面的文档

https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QRadioButton.html
https://doc.qt.io/qtforpython-6/PySide6/QtWidgets/QAbstractButton.html

QRadioButton使用

QRadioButton分组

在使用QRadioButton前需要对QRadioButton进行分组,我们可以利用QButtonGroup进行分组

		btn1 = QButtonGroup(self) # 创建ButtonGroup
        btn2 = QButtonGroup(self) # 创建ButtonGroup
        btn3 = QButtonGroup(self) # 创建ButtonGroup


        # 将RadioBox1/2/3/4 划入组1
        btn1.addButton(self.ui.radioButton,1)
        btn1.addButton(self.ui.radioButton_2,1)
        btn1.addButton(self.ui.radioButton_3,1)
        btn1.addButton(self.ui.radioButton_4,1)

        # 将RadioBox5/6/7/8 划入组2
        btn2.addButton(self.ui.radioButton_5,1)
        btn2.addButton(self.ui.radioButton_6,1)
        btn2.addButton(self.ui.radioButton_7,1)
        btn2.addButton(self.ui.radioButton_8,1)

		# 将RadioBox9/10 划入组3
        btn3.addButton(self.ui.radioButton_9,1)
        btn3.addButton(self.ui.radioButton_10,1)

QRadioButton设置文本

QRadioButton设置文本有两种方式,分别是代码设置和界面设置

代码设置

 		 # 设置RadioBox文本
        self.ui.radioButton.setText("Windows")
        self.ui.radioButton_2.setText("SQL")
        self.ui.radioButton_3.setText("Python")
        self.ui.radioButton_4.setText("Java")

        self.ui.radioButton_5.setText("十进制")
        self.ui.radioButton_6.setText("十六进制")
        self.ui.radioButton_7.setText("二进制")
        self.ui.radioButton_8.setText("八进制")

        self.ui.radioButton_9.setText("C语言")
        self.ui.radioButton_10.setText("C++语言")

界面设置

我们可以通过designer软件进行QRadioButton的文本设置,具体设置如下:
点击控件->属性编辑器->text里面进行设置
在这里插入图片描述

QRadioButton禁用和启用

当我们不需要使用某个QRadioButton的功能时,可以设置其为禁用模式,设置为禁用模式的QRadioButton将不能被用户使用。如果用户需要再次使用该QRadioButton,需要将其QRadioButton设置为启用模式。同样地设置QRadioButton的禁用和启用也可通过代码和界面进行设置。默认QRadioButton是启用模式。

代码设置

self.ui.radioButton_4.setEnabled(False) # 将单选框4设置为禁用模式
self.ui.radioButton_8.setEnabled(True)  # 将单选框8设置为启用模式

界面设置

我们可以通过designer软件进行QRadioButton的禁用和启用设置,具体设置如下:
点击控件->属性编辑器->enabled里面进行设置。打勾则代表该QRadioButton是启用状态,不打勾则代表该QRadioButton是禁用状态。
在这里插入图片描述

QRadioButton设置默认值

QRadioButton可以设置默认值,也就是说当软件启动时,QRadioButton是默认选中还是未选中状态。设置默认值可以通过代码和界面设置。

代码设置

self.ui.radioButton_9.setChecked(False) # 将单选框9的默认值为不选中
self.ui.radioButton_10.setChecked(True) # 将单选框10的默认值为选中

界面设置

我们可以通过designer软件进行QRadioButton的默认值设置,具体设置如下:
点击控件->属性编辑器->checked里面进行设置。打勾则代表该QRadioButton为选中,不打勾则代表该QRadioButton未选中。

在这里插入图片描述

读取QRadioButton状态

QRadioButton可以通过isChecked函数读取其状态,返回True则代表QRadioButton被选中,返回False则代表QRadioButton没有被选中。

	self.ui.pushButton.clicked.connect(self.pushButton_func)

    def pushButton_func(self):
        if self.ui.radioButton_9.isChecked() == True:
            print("C语言答案被选中")
        else:
            print("C语言答案没有被选中")

        if self.ui.radioButton_10.isChecked() == True:
            print("C++语言答案被选中")
        else:
            print("C++语言答案没有被选中")

QRadioButton样式设计

如果觉得QRadioButton的外观太单调,可以设置QRadioButton的样式,根据自己的需要设置其背景颜色,字体颜色等。同样地有两种方法进行样式设计,代码设置和界面设置

代码设置

self.ui.radioButton_9.setStyleSheet("#radioButton_9:hover{background-color:rgb(0,130,150);border:2px solid #5F92B2;border-radius:5px;color:white;}"
                                            "#radioButton_9:pressed{background-color:rgb(85,170,255);border:2px solid #3C80B1;border-radius:5px;color:red;}")

界面设置

我们可以通过designer软件进行QRadioButton的样式设置,具体设置如下:
点击控件->属性编辑器->styleSheet里面进行设置。
在这里插入图片描述

完整程序

界面程序

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>601</width>
    <height>244</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <widget class="QLabel" name="label">
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>20</height>
         </size>
        </property>
        <property name="text">
         <string>以下哪个是操作系统</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton">
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_2">
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_3">
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_4">
        <property name="enabled">
         <bool>true</bool>
        </property>
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QVBoxLayout" name="verticalLayout_2">
      <item>
       <widget class="QLabel" name="label_2">
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>20</height>
         </size>
        </property>
        <property name="text">
         <string>计算机内部使用的编码是</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_5">
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_6">
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_7">
        <property name="enabled">
         <bool>true</bool>
        </property>
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_8">
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QVBoxLayout" name="verticalLayout_3">
      <item>
       <widget class="QLabel" name="label_3">
        <property name="maximumSize">
         <size>
          <width>16777215</width>
          <height>20</height>
         </size>
        </property>
        <property name="text">
         <string>以下哪个是面向对象语言</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_9">
        <property name="text">
         <string>RadioButton</string>
        </property>
        <property name="checkable">
         <bool>true</bool>
        </property>
        <property name="checked">
         <bool>false</bool>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QRadioButton" name="radioButton_10">
        <property name="styleSheet">
         <string notr="true">#radioButton_10:hover{background-color:rgb(0,130,150);border:2px solid #5F92B2;border-radius:5px;color:white;}
#radioButton_10:pressed{background-color:rgb(85,170,255);border:2px solid #3C80B1;border-radius:5px;color:red;}</string>
        </property>
        <property name="text">
         <string>RadioButton</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>确认答案</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>601</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

主程序

# Import Qt libraries
from PySide6.QtWidgets import *
from PySide6.QtCore import QFile
# Import UI developed in Qt Creator
from radiobox_ui import Ui_MainWindow  # 导入界面
# Import PseudoSensor
# Import system tools and datetime
import sys
import statistics
import time
from datetime import datetime

# Create and start the Qt application
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        
        # 设置界面为用户设计的界面
        self.ui = Ui_MainWindow() 
        self.ui.setupUi(self) 

        btn1 = QButtonGroup(self) # 创建ButtonGroup
        btn2 = QButtonGroup(self) # 创建ButtonGroup
        btn3 = QButtonGroup(self) # 创建ButtonGroup


        # 将RadioBox1/2/3/4 划入组1
        btn1.addButton(self.ui.radioButton,1)
        btn1.addButton(self.ui.radioButton_2,1)
        btn1.addButton(self.ui.radioButton_3,1)
        btn1.addButton(self.ui.radioButton_4,1)

        # 将RadioBox5/6/7/8 划入组2
        btn2.addButton(self.ui.radioButton_5,1)
        btn2.addButton(self.ui.radioButton_6,1)
        btn2.addButton(self.ui.radioButton_7,1)
        btn2.addButton(self.ui.radioButton_8,1)

        # 将RadioBox9/10 划入组3
        btn3.addButton(self.ui.radioButton_9,1)
        btn3.addButton(self.ui.radioButton_10,1)
        

        # 设置RadioBox文本
        self.ui.radioButton.setText("Windows")
        self.ui.radioButton_2.setText("SQL")
        self.ui.radioButton_3.setText("Python")
        self.ui.radioButton_4.setText("Java")

        self.ui.radioButton_5.setText("十进制")
        self.ui.radioButton_6.setText("十六进制")
        self.ui.radioButton_7.setText("二进制")
        self.ui.radioButton_8.setText("八进制")

        self.ui.radioButton_9.setText("C语言")
        self.ui.radioButton_10.setText("C++语言")

        self.ui.radioButton_4.setEnabled(False) # 将单选框4设置为禁用模式
        self.ui.radioButton_8.setEnabled(True)  # 将单选框8设置为启用模式

        self.ui.radioButton_9.setChecked(False) # 将单选框9的默认值为不选中
        self.ui.radioButton_10.setChecked(True) # 将单选框10的默认值为选中

        self.ui.radioButton_9.setStyleSheet("#radioButton_9:hover{background-color:rgb(0,130,150);border:2px solid #5F92B2;border-radius:5px;color:white;}"
                                            "#radioButton_9:pressed{background-color:rgb(85,170,255);border:2px solid #3C80B1;border-radius:5px;color:red;}") # 设置单选框9样式
        
        self.ui.pushButton.clicked.connect(self.pushButton_func)


    def pushButton_func(self):
        if self.ui.radioButton_9.isChecked() == True: # 判断单选框9状态
            print("C语言答案被选中")
        else:
            print("C语言答案没有被选中")

        if self.ui.radioButton_10.isChecked() == True: # 判断单选框10状态
            print("C++语言答案被选中")
        else:
            print("C++语言答案没有被选中")

    def closeAndExit(self):
        sys.exit()

if __name__ == "__main__":
    app = QApplication(sys.argv) # 初始化QApplication

    # 初始化界面并显示界面
    window = MainWindow() 
    window.show() 

    sys.exit(app.exec())

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1072494.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

语音芯片基础知识 什么是语音芯 他有什么作用 发展趋势是什么

目录 一、语音芯片的简介 常见的语音芯片有哪些&#xff1f; 语音芯片的种类有很多&#xff0c;大体区分下来也就4个类别而已&#xff1a; 选型的经验说明如下&#xff1a; 推荐使用flash型语音芯片 一、语音芯片的简介 语音芯片基础知识&#xff1a; 什么是语音芯片&…

计算机竞赛 题目:基于深度学习的手势识别实现

文章目录 1 前言2 项目背景3 任务描述4 环境搭配5 项目实现5.1 准备数据5.2 构建网络5.3 开始训练5.4 模型评估 6 识别效果7 最后 1 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 基于深度学习的手势识别实现 该项目较为新颖&#xff0c;适合作为竞赛课题…

$attrs 和 $listeners (vue2vue3)

目录 透传 Attributes Attributes 继承​ 对 class 和 style 的合并 v-on 监听器继承 深层组件继承 禁用 Attributes 继承 多根节点的 Attributes 继承 vue2 $attrs 和 $listeners $attrs 概念说明 $attrs 案例 $listeners 概念说明 $listeners案例 vue3 $attr…

大数据学习(1)-Hadoop

&&大数据学习&& &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 承认自己的无知&#xff0c;乃是开启智慧的大门 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4dd;支持一下博>主哦&#x…

Agilent安捷伦3458A八位半万用表

Agilent 3458A突破了生产测试&#xff0c;科研与开发及校准实验室在速度与精度上长时期的性能 壁垒&#xff0c;是惠普公司提供的快速&#xff0c;灵活且精确的多用表。在你的系统中或工作台上&#xff0c; 3458A以空前的测试系统吞吐量和精度、七种功能的测量灵活性&#xff0…

Ubuntu 18.04 OpenCV3.4.5 + OpenCV3.4.5 Contrib 编译

目录 1 依赖安装 2 下载opencv3.4.5及opencv3.4.5 contrib版本 3 编译opencv3.4.5 opencv3.4.5_contrib及遇到的问题 1 依赖安装 首先安装编译工具CMake&#xff0c;命令安装即可&#xff1a; sudo apt install cmake 安装Eigen&#xff1a; sudo apt-get install libeigen3-…

解决Mysql8.0不存在mysql.proc表

摘自MySQL8.0官方文档&#xff1a; The parameters and routines data dictionary tables together supersede the proc table from before MySQL 8.0. 大概意思说&#xff0c;在mysql database中parameters表和routines数据字典表一起取代了MySQL 8.0之前的proc表。 MySQL 8.0…

前端uniapp生成海报并保存相册

uiapp插件 目录 图片qrcode.vue源码完整版封装源码qrcodeSwiper.vue最后 图片 qrcode.vue源码完整版 <template><view class"qrcode"><div class"qrcode_swiper SourceHanSansSC-Normal"><!-- <cc-scroolCard :dataInfo"dat…

巧用excel实现试卷向表格的转换

MID($E$10,FIND(D14,$E$10,1),FIND(D15,$E$10,1)-FIND(D14,$E$10,1)) MID($E$10,FIND(D15,$E$10,1),FIND(D16,$E$10,1)-FIND(D15,$E$10,1)) 中华人民共和国司法部

1130 - Host ‘192.168.10.10‘ is not allowed to connect to this MysOL server

mysql 远程登录报错误信息&#xff1a;1130 - Host 124.114.155.70 is not allowed to connect to this MysOL server //需要在mysql 数据库目录下修改 use mysql; //更改用户的登录主机为所有主机&#xff0c;%代表所有主机 update user set host% where userroot; //刷新权…

uni-app 实现考勤打卡功能

一、在页面中引入地图组件 <map id"map" style"width: 100%; height: 100%" :latitude"myLatitude" :longitude"myLongitude" :circles"circles" :markers"markers"> </map>属性名类型说明longitudeN…

IDEA自定义代码快捷指令

在IDEA中&#xff0c;有很多默认的代码快捷指令&#xff0c;例如输出&#xff08;sout&#xff09;、main方法&#xff08;psvm&#xff09;等&#xff0c;有时候&#xff0c;我们也需要自定义一些常用的代码片段&#xff0c;例如执行时间打印等&#xff0c;这时候&#xff0c;…

阿桂天山的技术小结:Flask对Ztree树节点搜索定位

话不多说,上图上源码 1.先看效果图 2.前端页面部分: 1)页面 <!DOCTYPE html> <HTML><HEAD><TITLE>Ewangda 阿桂天山的Ztree实战</TITLE><meta http-equiv"content-type" content"text/html; charsetUTF-8"><link…

vue脚手架项目创建及整理

环境准备 首先安装node,如果项目需要指定node版本 可以按装nvm控制版本 创建vue vue create 项目名选择对应版本 这边我是选的自定义&#xff0c;就是第三个选项&#xff0c;可以提前给我下好 router vuex什么的&#xff08;空格&#xff09; 选项如图标注 等待下载所需的…

什么是Spring

一、前言 参与java项目开发的工作&#xff0c;没有人可以离开Spring&#xff0c;但是什么是Spring呢&#xff1f;我们平时可以说对于这个概念早已经是熟视无睹。今天我还特意查看了官网的介绍&#xff0c;但是上面竟然没有说明Spring是什么&#xff0c;之说了Spring的特征和能…

这道面试题工作中经常碰到,但 99% 的程序员都答不上来

小时候都被问过一个脑筋急转弯&#xff0c;把大象放进冰箱有几个步骤&#xff1f;我们一开始都会抓耳挠腮&#xff0c;去想着该如何把大象塞进冰箱。最终揭晓的答案却根本不关心具体的操作方法&#xff0c;只是提供了 3 个步骤组成的流程&#xff0c;「把冰箱打开&#xff0c;把…

MQTT服务器源码解析

目录 1、关于header问题 2、MQTT 连接参数的使用 2.1连接地址 2.2 基于 TCP 的 MQTT 连接 2.3 基于 WebSocket 的连接 3、订阅topic 4、推送消息给订阅者 5、QOS 机制 5.1 QOS是什么 5.2 QOS的实现原理 5.3 发送流程 6、reatain机制 总结&#xff1a;给还没上线的…

节日灯饰灯串灯出口欧洲CE认证检测

灯串&#xff08;灯带&#xff09;&#xff0c;这个产品的形状就象一根带子一样&#xff0c;再加上产品的主要原件就是LED&#xff0c;因此叫做灯串或者灯带。2022年&#xff0c;我国灯具及相关配件产品出口总额超过460亿美元。其中北美是最大的出口市场。其次是欧洲市场&#…

传奇开服教程GOM传奇引擎外网全套架设教程

传奇开服教程&#xff1a;GOM引擎外网架设教程 准备工具&#xff1a;版本&#xff0c;DBC数据库&#xff0c;传奇客户端&#xff0c;服务器&#xff0c;备案域名 架设传奇外网GOM引擎版本之前我们连接登录服务器&#xff0c;我们把版本&#xff0c;DBC数据库&#xff0c;传奇…