简体   繁体   English

如何在 PyQt5 中为一组 QLabels 设置背景颜色?

[英]How to set a background color to a set of QLabels In PyQt5?

I have used so many QLabels( 20 Labels ) in a single Form.我在一个表单中使用了这么多 QLabels(20 个标签)。 I want to set a background color to red for the first 5 labels and blue for the next 5 labels and green for the remaining?我想将前 5 个标签的背景颜色设置为红色,后 5 个标签设置为蓝色,其余标签设置为绿色? Based on the QWidget property, we set style sheets like background color, foreground color etc.基于 QWidget 属性,我们设置样式表,如背景色、前景色等。

import sys

from PyQt5.QtWidgets import *

class StyleSheet(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Style Sheet Example")
        self.UI()
        self.layouts()
        self.show()

    def UI(self):
        self.lbl_redtext1 = QLabel("Red Text 1")
        self.lbl_redtext2 = QLabel("Red Text 2")
        self.lbl_redtext3 = QLabel("Red Text 3")

        self.lbl_bluetext1 = QLabel("Blue Text 1")
        self.lbl_bluetext2 = QLabel("Blue Text 2")
        self.lbl_bluetext3 = QLabel("Blue Text 3")

        self.lbl_greentext1 = QLabel("Green Text 1")
        self.lbl_ornagetext1 = QLabel("Orange Text 1")
        self.lbl_greentext2 = QLabel("Green text 2")
        self.lbl_redtext4 = QLabel("Red Text 4")

    def layouts(self):
        mainlayout = QHBoxLayout()
        leftlayout = QVBoxLayout()
        middlelayout = QVBoxLayout()
        rightllayout = QVBoxLayout()

        # All text In Red Color
        leftlayout.addWidget(self.lbl_redtext1)
        leftlayout.addWidget(self.lbl_redtext2)
        leftlayout.addWidget(self.lbl_redtext3)

        # all text in Blue Color
        middlelayout.addWidget(self.lbl_bluetext1)
        middlelayout.addWidget(self.lbl_bluetext2)
        middlelayout.addWidget(self.lbl_bluetext3)

        # text in green , orange and red color based on property
        rightllayout.addWidget(self.lbl_greentext1)
        rightllayout.addWidget(self.lbl_ornagetext1)
        rightllayout.addWidget(self.lbl_greentext2)
        rightllayout.addWidget(self.lbl_redtext4)

        mainlayout.addLayout(leftlayout)
        mainlayout.addLayout(middlelayout)
        mainlayout.addLayout(rightllayout)

        widget = QWidget()
        widget.setLayout(mainlayout)
        self.setCentralWidget(widget)

def main():
    app = QApplication(sys.argv)
    mainwindow = StyleSheet()
    sys.exit(app.exec_())

if __name__ =="__main__":
    main()

use/try setProperty method使用/尝试setProperty方法

import sys

from PyQt5.QtWidgets import *

class StyleSheet(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Style Sheet Example")
        self.UI()
        self.layouts()
        self.show()

    def UI(self):
        self.lbl_redtext1 = QLabel("Red Text 1")
        self.lbl_redtext1.setProperty("color","red")
        self.lbl_redtext2 = QLabel("Red Text 2")
        self.lbl_redtext2.setProperty("color", "red")
        self.lbl_redtext3 = QLabel("Red Text 3")
        self.lbl_redtext3.setProperty("color", "red")

        self.lbl_bluetext1 = QLabel("Blue Text 1")
        self.lbl_bluetext1.setProperty("color", "blue")
        self.lbl_bluetext2 = QLabel("Blue Text 2")
        self.lbl_bluetext2.setProperty("color", "blue")
        self.lbl_bluetext3 = QLabel("Blue Text 3")
        self.lbl_bluetext3.setProperty("color", "blue")

        self.lbl_greentext1 = QLabel("Green Text 1")
        self.lbl_greentext1.setProperty("color", "green")
        self.lbl_ornagetext1 = QLabel("Orange Text 1")
        self.lbl_ornagetext1.setProperty("color","orange")

        self.lbl_greentext2 = QLabel("Green text 2")
        self.lbl_greentext2.setProperty("color", "green")
        self.lbl_redtext4 = QLabel("Red Text 4")
        self.lbl_redtext4.setProperty("color", "red")
        qApp.setStyleSheet(self.colour_stylesheet())

    def colour_stylesheet(self):
        return """
                 QLabel[color="red"]
                 {
                 background-color:red;
                 font:15pt Trebuchet MS;
                 color:White;

                 }

                 QLabel[color = "blue"]
                 {
                 background-color:blue;
                 font: 15pt Trebuchet MS;
                 color: White;
                 }

                QLabel[color = "green"]
                 {
                 background-color:green;
                 font: 15pt Trebuchet MS;
                 color: White;
                 }
                 QLabel[color = "orange"]
                 {
                 background-color:orange;
                 font: 15pt Trebuchet MS;
                 color: balck;
                 }


                 """

    def layouts(self):
        mainlayout = QHBoxLayout()
        leftlayout = QVBoxLayout()
        middlelayout = QVBoxLayout()
        rightllayout = QVBoxLayout()

        # All text In Red Color
        leftlayout.addWidget(self.lbl_redtext1)
        leftlayout.addWidget(self.lbl_redtext2)
        leftlayout.addWidget(self.lbl_redtext3)

        # all text in Blue Color
        middlelayout.addWidget(self.lbl_bluetext1)
        middlelayout.addWidget(self.lbl_bluetext2)
        middlelayout.addWidget(self.lbl_bluetext3)

        # text in green , orange and red color based on property
        rightllayout.addWidget(self.lbl_greentext1)
        rightllayout.addWidget(self.lbl_ornagetext1)
        rightllayout.addWidget(self.lbl_greentext2)
        rightllayout.addWidget(self.lbl_redtext4)

        mainlayout.addLayout(leftlayout)
        mainlayout.addLayout(middlelayout)
        mainlayout.addLayout(rightllayout)

        widget = QWidget()
        widget.setLayout(mainlayout)
        self.setCentralWidget(widget)

def main():
    app = QApplication(sys.argv)
    mainwindow = StyleSheet()
    sys.exit(app.exec_())

if __name__ =="__main__":
    main()

you should use setObjectName on each label (eg: self.lbl_redtext1.setObjectName("red") ), that will set a name or id (css) to the label then customize them according to their label, here is how it will look like: you should use setObjectName on each label (eg: self.lbl_redtext1.setObjectName("red") ), that will set a name or id (css) to the label then customize them according to their label, here is how it will look like :

....
    def UI(self):
        ....
        self.lbl_redtext1.setObjectName("red")
        self.lbl_redtext2.setObjectName("red")
        self.lbl_redtext3.setObjectName("red")
        ....
        self.lbl_bluetext1.setObjectName("blue")
        self.lbl_bluetext1.setObjectName("blue")
        ....

    def layouts(self):
        ...
        widget.setStyleSheet("""
            QLabel#red{
                  background-color: red;
            } 
            QLabel#blue{
                  background-color:blue;
            }
        """)

and do same with others和别人做同样的事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM