我会详细介绍一下PyQt5中如何实现右侧的单选按钮指示灯。
简介
在PyQt5中,我们可以使用QButtonGroup和QRadioButton来实现单选按钮的控制。而对于右侧的指示灯,我们可以使用QLabel和QPixmap来实现。
实现步骤
- 创建QButtonGroup对象,并将需要进行单选的QRadioButton对象添加到QButtonGroup中:
python
group = QButtonGroup()
radio1 = QRadioButton('Radio Button 1')
radio2 = QRadioButton('Radio Button 2')
group.addButton(radio1)
group.addButton(radio2)
- 创建QLabel对象,并设置其背景颜色为灰色:
python
label = QLabel()
label.setStyleSheet('background-color: gray')
- 创建QPixmap对象,并设置其大小与QLabel对象相同:
python
pixmap = QPixmap(QSize(16, 16))
- 将QPixmap对象填充为红色,并将其设置为QLabel对象的图像:
python
pixmap.fill(QColor(255, 0, 0))
label.setPixmap(pixmap)
- 将QButtonGroup的buttonClicked()信号与更改QLabel图像的操作连接:
python
group.buttonClicked.connect(lambda: changeLabelImg(group.checkedId(), label, pixmap))
- 实现changeLabelImg()函数,根据选中的单选按钮来更改QLabel对象的图像:
python
def changeLabelImg(buttonId: int, label: QLabel, pixmap: QPixmap):
if buttonId == 1:
pixmap.fill(QColor(255, 0, 0))
elif buttonId == 2:
pixmap.fill(QColor(0, 255, 0))
label.setPixmap(pixmap)
至此,右侧的单选按钮指示灯已经实现成功。
示例说明
示例1
以下代码演示了如何创建两个单选按钮,并将它们添加到QButtonGroup中,然后实现右侧的红绿指示灯:
from PyQt5.QtWidgets import QWidget, QRadioButton, QButtonGroup, QLabel, QHBoxLayout, QVBoxLayout, QApplication
from PyQt5.QtGui import QPixmap, QColor
from PyQt5.QtCore import QSize
import sys
def changeLabelImg(buttonId: int, label: QLabel, pixmap: QPixmap):
if buttonId == 1:
pixmap.fill(QColor(255, 0, 0))
elif buttonId == 2:
pixmap.fill(QColor(0, 255, 0))
label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout()
buttonGroup = QButtonGroup()
radio1 = QRadioButton('Radio Button 1')
radio2 = QRadioButton('Radio Button 2')
buttonGroup.addButton(radio1)
buttonGroup.addButton(radio2)
label = QLabel()
label.setStyleSheet('background-color: gray')
pixmap = QPixmap(QSize(16, 16))
pixmap.fill(QColor(255, 0, 0))
label.setPixmap(pixmap)
buttonGroup.buttonClicked.connect(lambda: changeLabelImg(buttonGroup.checkedId(), label, pixmap))
hbox = QHBoxLayout()
hbox.addWidget(radio1)
hbox.addWidget(radio2)
layout.addLayout(hbox)
layout.addWidget(label)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
示例2
以下代码演示了如何实现多个单选按钮,每个单选按钮有不同的指示灯颜色:
from PyQt5.QtWidgets import QWidget, QRadioButton, QButtonGroup, QLabel, QHBoxLayout, QVBoxLayout, QApplication
from PyQt5.QtGui import QPixmap, QColor
from PyQt5.QtCore import QSize
import sys
def changeLabelImg(buttonId: int, label: QLabel, pixmap: QPixmap, pixmapList: list):
pixmap.fill(pixmapList[buttonId - 1])
label.setPixmap(pixmap)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout()
buttonGroup = QButtonGroup()
radio1 = QRadioButton('Red')
radio2 = QRadioButton('Green')
radio3 = QRadioButton('Blue')
buttonGroup.addButton(radio1, 1)
buttonGroup.addButton(radio2, 2)
buttonGroup.addButton(radio3, 3)
label = QLabel()
label.setStyleSheet('background-color: gray')
pixmap = QPixmap(QSize(16, 16))
pixmapList = [QColor(255, 0, 0), QColor(0, 255, 0), QColor(0, 0, 255)]
pixmap.fill(pixmapList[0])
label.setPixmap(pixmap)
buttonGroup.buttonClicked.connect(lambda: changeLabelImg(buttonGroup.checkedId(), label, pixmap, pixmapList))
hbox1 = QHBoxLayout()
hbox1.addWidget(radio1)
hbox1.addWidget(radio2)
hbox1.addWidget(radio3)
layout.addLayout(hbox1)
layout.addWidget(label)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
在这个示例中,我们在QButtonGroup中设定了每个QRadioButton的Id,并使用pixmapList来保存每个单选按钮对应的指示灯颜色信息。在changeLabelImg()函数中,我们根据选中的单选按钮来获取对应的颜色信息,然后填充pixmap并将其设置为QLabel的图像。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:PyQt5 – 右侧的单选按钮指示灯 - Python技术站