PyQt5基础学习-QRadioButton单选按钮 1.QRadioButton().setChecked(设置起始状态) 2.QRadioButton().toggled.connect(连接状态变化时的函数) 3.self.sender(获得当前的焦点变化按钮)
使用QRadioButton().toggled.connect连接需要变化的函数,在函数中通过判断单选框状态()来self.sender().isChecked()进行变化
QRadioButtonDemo.py
""" 单选按钮控件(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.button3 = QRadioButton("单选按钮3") self.button3.toggled.connect(self.buttonState) layout.addWidget(self.button3) 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_())
每天更新pyQt5内容