QRadioButton基本操作和槽函数
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Radiodemo(QWidget):
def __init__(self, parent=None):
super(Radiodemo, self).__init__(parent)
layout = QHBoxLayout()
self.btn1 = QRadioButton("Button1")
self.btn1.setChecked(True)
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):
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_())
多组互斥
import sys
from PyQt5.Qt import *
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("QButtonGroup的使用")
window.resize(500, 500)
window.move(400, 250)
rb_male = QRadioButton("男", window)
rb_male.move(100, 100)
rb_female = QRadioButton("女", window)
rb_female.move(180, 100)
sex_group = QButtonGroup(window)
sex_group.addButton(rb_male) # 把rb_male按钮添加到sex_group按钮组
sex_group.addButton(rb_female) # 把rb_female按钮添加到sex_group按钮组
rb_yes = QRadioButton("yes", window)
rb_yes.move(100, 220)
rb_no = QRadioButton("no", window)
rb_no.move(180, 220)
answer_group = QButtonGroup(window)
answer_group.addButton(rb_yes)
answer_group.addButton(rb_no)
"""
男、女属于同一按钮组,互斥,只能选中其中之一
yes、no属于另一按钮组,不受男女的影响
"""
window.show()
sys.exit(app.exec_())