I have used so many QLabels( 20 Labels ) in a single Form. 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? Based on the QWidget property, we set style sheets like background color, foreground color etc.
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
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:
....
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.