PyQt5 – 为按下的单选按钮设置皮肤

  • Post category:Python

那我来给你详细讲解一下。

PyQt5 – 为按下的单选按钮设置皮肤

在使用PyQt5的过程中,我们可能需要为按下的单选按钮设置一些特定的皮肤(背景颜色、边框颜色等),以及根据选项的不同改变UI的状态。这里,我们可以通过QButtonGroup类和QSS(Qt样式表)来实现这个功能。

步骤1:创建GUI和QButtonGroup对象

在创建GUI界面时,我们需要添加一组单选按钮,并创建一个QButtonGroup对象。这个QButtonGroup对象会自动将所有添加到组内的单选按钮连接起来,从而可以轻松地进行读取和管理。

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

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

        self.button1 = QRadioButton("Button 1")
        self.button2 = QRadioButton("Button 2")
        self.button3 = QRadioButton("Button 3")

        self.button_group = QButtonGroup()

        self.button_layout = QVBoxLayout()
        self.button_layout.addWidget(self.button1)
        self.button_layout.addWidget(self.button2)
        self.button_layout.addWidget(self.button3)
        self.setLayout(self.button_layout)

        self.button_group.addButton(self.button1)
        self.button_group.addButton(self.button2)
        self.button_group.addButton(self.button3)

步骤2:使用QSS设置按钮的皮肤

QSS是一种类似于CSS的样式表语言,可以用来设置PyQt5窗口小部件的样式。这里,我们可以通过QSS来设置按钮的背景颜色、文字颜色、边框等属性。QSS的语法类似于CSS,可以使用各种选择器和属性。

self.button1.setStyleSheet("QRadioButton::indicator:checked {background-color: red; border: 1px solid black;}")
self.button2.setStyleSheet("QRadioButton::indicator:checked {background-color: blue; border: 1px solid black;}")
self.button3.setStyleSheet("QRadioButton::indicator:checked {background-color: yellow; border: 1px solid black;}")

我们可以使用setStyleSheet()方法来设置单选按钮的QSS样式。在样式表中,QRadioButton::indicator选择器用于选择单选按钮,checked伪类用于选择按下的按钮。

上述样式表中,我们为三个单选按钮分别设置了红色、蓝色和黄色的背景颜色,以及1像素的黑色实线边框。

步骤3:根据选项的不同改变UI的状态

我们可以使用QButtonGroup类的buttonClicked信号来连接一个槽函数,从而在按下的按钮变化时改变UI的状态。

def slot_button_clicked(self):
    if self.button1.isChecked():
        print("Button 1 is selected")
    elif self.button2.isChecked():
        print("Button 2 is selected")
    elif self.button3.isChecked():
        print("Button 3 is selected")

self.button_group.buttonClicked.connect(self.slot_button_clicked)

在槽函数中,我们可以使用isChecked()方法来检查哪个单选按钮被选择。如果“Button 1”被选中,则输出“Button 1 is selected”等字样。

示例1:为单选按钮设置状态颜色

这是一个简单的示例,它演示了如何为单选按钮设置不同状态的颜色。

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

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

        self.button1 = QRadioButton("Button 1")
        self.button2 = QRadioButton("Button 2")
        self.button3 = QRadioButton("Button 3")

        self.button_group = QButtonGroup()

        self.button_layout = QVBoxLayout()
        self.button_layout.addWidget(self.button1)
        self.button_layout.addWidget(self.button2)
        self.button_layout.addWidget(self.button3)
        self.setLayout(self.button_layout)

        self.button_group.addButton(self.button1)
        self.button_group.addButton(self.button2)
        self.button_group.addButton(self.button3)

        self.button1.setStyleSheet("QRadioButton::indicator:checked {background-color: red;}")
        self.button2.setStyleSheet("QRadioButton::indicator:checked {background-color: green;}")
        self.button3.setStyleSheet("QRadioButton::indicator:checked {background-color: blue;}")

        self.button_group.buttonClicked.connect(self.slot_button_clicked)

    def slot_button_clicked(self):
        if self.button1.isChecked():
            self.button1.setStyleSheet("QRadioButton::indicator:checked {background-color: red;}")
            self.button2.setStyleSheet("QRadioButton::indicator:checked {background-color: none;}")
            self.button3.setStyleSheet("QRadioButton::indicator:checked {background-color: none;}")
        elif self.button2.isChecked():
            self.button1.setStyleSheet("QRadioButton::indicator:checked {background-color: none;}")
            self.button2.setStyleSheet("QRadioButton::indicator:checked {background-color: green;}")
            self.button3.setStyleSheet("QRadioButton::indicator:checked {background-color: none;}")
        elif self.button3.isChecked():
            self.button1.setStyleSheet("QRadioButton::indicator:checked {background-color: none;}")
            self.button2.setStyleSheet("QRadioButton::indicator:checked {background-color: none;}")
            self.button3.setStyleSheet("QRadioButton::indicator:checked {background-color: blue;}")

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

在这个示例中,我们为三个单选按钮设置了不同的状态颜色。当其中一个按钮被按下时,其他按钮的背景色将变为none,同时按下的按钮的背景色将改变为特殊的颜色。

示例2:为单选按钮设置圆形的指示器

这是另一个示例,它演示了如何为单选按钮添加一个圆形的指示器,以及如何为指示器设置不同的颜色。

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

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

        self.button1 = QRadioButton("Button 1")
        self.button2 = QRadioButton("Button 2")
        self.button3 = QRadioButton("Button 3")

        self.button_group = QButtonGroup()

        self.button_layout = QVBoxLayout()
        self.button_layout.addWidget(self.button1)
        self.button_layout.addWidget(self.button2)
        self.button_layout.addWidget(self.button3)
        self.setLayout(self.button_layout)

        self.button_group.addButton(self.button1)
        self.button_group.addButton(self.button2)
        self.button_group.addButton(self.button3)

        self.button1.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
        self.button2.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
        self.button3.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")

        self.button1.setStyleSheet("QRadioButton::indicator:checked {background-color: red;}")
        self.button2.setStyleSheet("QRadioButton::indicator:checked {background-color: green;}")
        self.button3.setStyleSheet("QRadioButton::indicator:checked {background-color: blue;}")

        self.button_group.buttonClicked.connect(self.slot_button_clicked)

    def slot_button_clicked(self):
        if self.button1.isChecked():
            self.button1.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: red;}")
            self.button2.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
            self.button3.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
        elif self.button2.isChecked():
            self.button1.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
            self.button2.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: green;}")
            self.button3.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
        elif self.button3.isChecked():
            self.button1.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
            self.button2.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: none;}")
            self.button3.setStyleSheet("QRadioButton::indicator {width: 10px; height: 10px; border-radius: 5px; border: 2px solid black; background-color: blue;}")

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

在这个示例中,我们为三个单选按钮添加了一个圆形的指示器,并为指示器设置了不同的样式。在样式表中,我们使用了width和height属性来设置指示器的大小,使用border-radius属性来设置圆角半径,使用border属性来设置边框的样式和颜色,使用background-color属性来设置指示器的背景颜色。当其中一个按钮被按下时,其他按钮的背景色将变为none,同时按下的按钮的指示器背景色将改变为特殊的颜色。

以上就是关于PyQt5 – 为按下的单选按钮设置皮肤的完整使用攻略,希望你能够理解。