python GUI库图形界面开发之PyQt5单选按钮控件QRadioButton详细使用方法与实例
2020-03-11 15:58
1921 查看
PyQt5单选按钮控件QRadioButton简介
QRadioButton 继承自 QAbstractButton,其主要作用提供用户一些互斥的按钮。
QRadioButton 只有一个的时候,功能类似于复选框, 可以选择和取消,但是如果有多个,则必须有一个被选中
QRadioButton类中常用的方法
方法 | 描述 |
---|---|
setCheckanle() | 设置按钮是否已经被选中,可以改变单选按钮的选中状态,如果设置为True则表示单选按钮将保持以点击和释放状态 |
isChecked() | 返回单选按钮的状态,返回值True或False |
setText() | 设置单选按钮显示的文本 |
text() | 返回单选按钮显示的文本 |
QRadioButton按钮的使用实例
import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * class RadioDemo(QWidget): def __init__(self,parent=None): super(RadioDemo,self).__init__(parent) #水平布局 layout=QHBoxLayout() self.btn1=QRadioButton('Button1') #默认选中btn1self.btn1.setChecked(True) #toggled信号与槽函数绑定 self.btn1.toggled.connect(lambda :self.btnstate(self.btn1)) layout.addWidget(self.btn1) self.btn2 = QRadioButton('Button2') self.btn2.toggled.connect(lambda: self.btnstate(self.btn2)) layout.addWidget(self.btn2) self.setLayout(layout) self.setWindowTitle('RadioButton demo') def btnstate(self,btn): #输出按钮1与按钮2的状态,选中还是没选中 if btn.text()=='Button1': if btn.isChecked()==True: print(btn.text()+"is selected") else: print(btn.text()+"is deselected") if btn.text()=="Button2": if btn.isChecked() == True: print(btn.text() + "is selected") else: print(btn.text() + "is deselected") if __name__ == '__main__': app=QApplication(sys.argv) radioDemo=RadioDemo() radioDemo.show() sys.exit(app.exec_())
效果如图
QRadioButton代码分析
在这个例子中,两个互斥的单选框被放置在窗口中
第一个单选按钮btn1,被设置成默认状态
self.btn1.setChecked(True)
当选择两个按钮相互切换时,按钮的状态发生改变,将触发toggle信号,并与槽函数btnstate()连接。使用lamdba的方式允许将源信号传递给槽函数,将按钮作为参数
self.btn1.toggled.connect(lambda :self.btnstate(self.btn1)) self.btn1.toggled.connect(lambda :self.btnstate(self.btn1))
当发射toggled信号后,使用btnstate()函数来检查按钮的状态
所有QRadioButton的控件是互斥的,所以也可以通过多个父控件来对QRadioButton进行分组。不过,一般使用QButtonGroup来对按键进行分组。通过按键分组的方式来让不同类型的QRadioButton不互相排斥。
本文主要讲解了PyQt5单选按钮控件QRadioButton详细使用方法与实例,更多关于PyQt5控件知识请查看下面的相关链接
您可能感兴趣的文章:
- python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例
- python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
- Pyqt5自适应布局实例
- 浅谈pyqt5在QMainWindow中布局的问题
- PyQt5每天必学之布局管理
- python GUI库图形界面开发之PyQt5滚动条控件QScrollBar详细使用方法与实例
- python GUI库图形界面开发之PyQt5 MDI(多文档窗口)QMidArea详细使用方法与实例
- python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
- python GUI库图形界面开发之PyQt5选项卡控件QTabWidget详细使用方法与实例
- python GUI库图形界面开发之PyQt5表格控件QTableView详细使用方法与实例
- python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
- python GUI库图形界面开发之PyQt5状态栏控件QStatusBar详细使用方法实例
- python GUI库图形界面开发之PyQt5计数器控件QSpinBox详细使用方法与实例
- python GUI库图形界面开发之PyQt5切换按钮控件QPushButton详细使用方法与实例
- python GUI库图形界面开发之PyQt5多行文本框控件QTextEdit详细使用方法实例
- python GUI库图形界面开发之PyQt5复选框控件QCheckBox详细使用方法与实例
- python GUI库图形界面开发之PyQt5布局控件QVBoxLayout详细使用方法与实例
相关文章推荐
- python GUI库图形界面开发之PyQt5切换按钮控件QPushButton详细使用方法与实例
- python GUI库图形界面开发之PyQt5状态栏控件QStatusBar详细使用方法实例
- python GUI库图形界面开发之PyQt5计数器控件QSpinBox详细使用方法与实例
- python GUI库图形界面开发之PyQt5多行文本框控件QTextEdit详细使用方法实例
- python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
- 【Android 开发】:UI控件之单选按钮 RadioButton 的使用方法
- Python中PyQt5/PySide2的按钮控件使用实例
- Android程序开发中单选按钮(RadioGroup)的使用详解
- ASP.NET中 RadioButtonList 单选按钮组控件的使用方法
- Python3 Tkinter基础 LabelFrame Radiobutton 形成两组不相互限制的单选按钮
- 使用easyui框架form控件,单选按钮radio或复选框checkbox样式问题
- 在GridView里使用单选按钮,RadioButton/Radio
- Android控件系列之RadioButton与RadioGroup使用方法 详细出处参考:http://www.jb51.net/article/31799.htm
- android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
- Qt5-控件-QRadioButton-单选按钮-用于从多个选项中选取一个-单选神器
- 【WPF开发备忘】使用MVVM模式开发中列表控件内的按钮事件无法触发解决方法
- 使用easyui框架form控件,单选按钮radio或复选框checkbox样式问题
- 在GridView里使用单选按钮,RadioButton/Radio
- 在GridView里使用单选按钮,RadioButton/Radio
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法