import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class RadioButtonDemo(QWidget):
def __init__(self):
super(RadioButtonDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QRadioButton')
self.button1 = QRadioButton('单选按钮1')
self.button1.setChecked(True)
self.button1.toggled.connect(self.buttonState)
self.button2 = QRadioButton('单选按钮2')
self.button2.toggled.connect(self.buttonState)
layout = QHBoxLayout()
layout.addWidget(self.button1)
layout.addWidget(self.button2)
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)
win = RadioButtonDemo()
win.show()
sys.exit(app.exec_())
38 单选按钮控件(QRadioButton)
最新推荐文章于 2024-05-05 09:24:46 发布