def initUI(self):
self.setWindowTitle('RadionButtonDemo')
self.setGeometry(500,500,300,200)
button1 = QRadioButton('radiobutton 1')
button1.setChecked(True)
button1.toggled.connect(lambda :self.buttonState(button1)) #这个是一个状态切换(这个会触发两个事件,点下,松开)
# button1.clicked.connect(lambda: self.buttonState(button1)) #这个只是一次点击事件
button2 = QRadioButton('radiobutton 2')
button2.toggled.connect(lambda :self.buttonState(button2))
layout = QVBoxLayout()
layout.addWidget(button1)
layout.addWidget(button2)
self.setLayout(layout)
def buttonState(self, btn):
radioButton = self.sender() #收到信号,返回值是对象?(大项目中需要集中判断吗?
if radioButton.text() == btn.text():#btn.text()按钮的名字
if btn.isChecked() == True: #判断状态 也可以是 radioButton.isChecked()
print('<'+btn.text()+'>'+'被选中')
else:
print('<' + btn.text() + '>' + '没有选中')
9.RadioButton控件
RadioButton状态切换实例
最新推荐文章于 2025-02-17 22:57:48 发布
本文介绍了一个使用QRadioButton组件创建的简单界面示例,演示了如何通过连接信号与槽来响应RadioButton的状态变化,并打印出被选中或未选中的RadioButton名称。

2万+

被折叠的 条评论
为什么被折叠?



