PyQt5基本窗口控件-----QRadioButton
程序员文章站
2022-05-28 11:02:58
...
PyQt5学习之路,点击有pyqt5学习笔记
QRadioButton类提供了一组可供选择的按钮和文本标签,为用户提供多选一的操作。
import sys
from PyQt5.QtWidgets import *
class QRadioButtonDemo(QWidget):
def __init__(self):
super(QRadioButtonDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QRdaioButton Demo')
self.resize(300, 300)
layout = QHBoxLayout()
self.button1 = QRadioButton('单选按钮1')
# 设置选中状态
self.button1.setChecked(True)
self.button2 = QRadioButton('单选按钮2')
self.button1.toggled.connect(self.buttonState)
self.button2.toggled.connect(self.buttonState)
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)
main = QRadioButtonDemo()
main.show()
sys.exit(app.exec_())
下一篇: 记一次OOM异常及处理