PyQt5 – 改变选中状态下被按下的单选按钮的颜色

  • Post category:Python

PyQt5是Python的GUI库之一,它允许开发者快速构建跨平台的图形用户界面。在PyQt5中,我们可以使用QRadioButton类来创建单选按钮。它可以被设置为选中或未选中状态。在选中状态下,可以通过改变颜色等方式来强调单选按钮的状态。下面是PyQt5修改选中状态下被按下的单选按钮颜色的完整使用攻略:

步骤1:导入PyQt5库和需要的模块

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

步骤2:创建单选按钮并绑定事件

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()

        # 创建两个单选按钮,分别是“是”和“否”
        self.yes_button = QRadioButton("是")
        self.no_button = QRadioButton("否")

        # 为两个单选按钮绑定clicked事件
        self.yes_button.clicked.connect(self.on_button_clicked)
        self.no_button.clicked.connect(self.on_button_clicked)

    # 当按钮被单击时,执行该方法
    def on_button_clicked(self):
        # 获取当前被选中的单选按钮
        checked_button = self.sender()
        # 如果该单选按钮被选中,则改变其背景色为蓝色,否则为灰色
        if checked_button.isChecked():
            checked_button.setStyleSheet("background-color: blue;")
        else:
            checked_button.setStyleSheet("background-color: gray;")

步骤3:创建并运行应用程序

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

上述示例代码中,我们创建了一个窗口并在该窗口中添加了两个单选按钮“是”和“否”。当单选按钮被单击时,会执行on_button_clicked()方法。在此方法中,我们获取当前被选中的单选按钮,并根据其选中状态修改其背景色。最后,我们创建并运行该应用程序。

另一个示例是在通过QButtonGroup添加多个单选按钮时,实现单选按钮的颜色随选中状态改变。代码如下:

步骤1:导入PyQt5库和需要的模块

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

步骤2:创建单选按钮并绑定事件

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()

        # 创建三个单选按钮,并添加到QButtonGroup中
        self.button_group = QButtonGroup()
        self.button_group.setExclusive(True)  # 设置按钮为互斥,即只能选中一个
        self.button1 = QRadioButton("选项1")
        self.button_group.addButton(self.button1)
        self.button2 = QRadioButton("选项2")
        self.button_group.addButton(self.button2)
        self.button3 = QRadioButton("选项3")
        self.button_group.addButton(self.button3)

        # 在垂直布局器中添加三个单选按钮
        layout = QVBoxLayout()
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        layout.addWidget(self.button3)

        # 为QButtonGroup绑定按钮clicked事件和idClicked事件
        self.button_group.buttonClicked.connect(self.on_button_clicked)
        self.button_group.idClicked[int].connect(self.on_id_clicked)

    # 当按钮被单击时,执行该方法
    def on_button_clicked(self, button):
        # 改变选中按钮的背景色为红色,未选中按钮背景色为白色
        for btn in self.button_group.buttons():
            if btn is button:
                btn.setStyleSheet("background-color: red;")
            else:
                btn.setStyleSheet("background-color: white;")

    # 当按钮id被单击时,执行该方法
    def on_id_clicked(self, button_id):
        # 通过id获取当前选中的按钮,并将其背景色设为绿色
        checked_button = self.button_group.button(button_id)
        checked_button.setStyleSheet("background-color: green;")

步骤3:创建并运行应用程序

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

上述示例代码中,我们创建了一个窗口并创建了三个单选按钮,将其添加到QButtonGroup中,并在垂直布局器中进行添加。我们为QButtonGroup绑定了buttonClicked和idClicked事件。当单选按钮被单击时,会执行on_button_clicked()方法,该方法会将已选中的按钮背景色改为红色,未选中的背景色改为白色。当单选按钮被选中时,会执行on_id_clicked()方法,该方法会获取当前选中的按钮,并修改其背景色为绿色。最后,我们创建并运行该应用程序。