import sys from PyQt5.QtWidgets import QApplication,QWidget,QRadioButton,QHBoxLayout class radiobuttonDemo(QWidget): def __init__(self): super(radiobuttonDemo, self).__init__() #定义radio button self.radioButton1 = QRadioButton() self.radioButton2 = QRadioButton() #radioButton1设置 #命名radiButton1 self.radioButton1.setText("Radio1") #setChecked设置已经点击 #radioButton1.setChecked(True) #radioButton1.setCheckable(True) self.radioButton1.toggled.connect(self.checkRadioState) #radioButton2设置 self.radioButton2.setText("Radio2") #布局 hBoxlayout = QHBoxLayout() hBoxlayout.addWidget(self.radioButton1) hBoxlayout.addWidget(self.radioButton2) self.setLayout(hBoxlayout) self.setWindowTitle("RadioButtonDemo") self.resize(200,200) def checkRadioState(self): if self.radioButton1.isChecked(): print("{} is Checked".format(self.radioButton1.text())) else: print("{} is not Checked".format(self.radioButton1.text())) if __name__ == '__main__': app = QApplication(sys.argv) radioButton = radiobuttonDemo() radioButton.show() sys.exit(app.exec_())
参考:PyQt5基本控件详解之QRadioButton(七)_jia666666的博客-CSDN博客_pyqt5 radiobutton