How to create a Stylesheet for Mulitiple QLabels or Multiple Qwidgets in PyQt5?
For example, In my programme, I use 8 QLabels, Out of Which 4 QLabels belong to one family/group and another 4 have a another family/group. Now I want to create a separate style sheet for both families. First family (all 4 QLabels) have to set the same property ( Background color, foreground color, Alignment, and font) and the second family also have to set different properties.
import sys
from PyQt5.QtWidgets import QWidget,QApplication,QLabel,QFormLayout
class Stylesheet_001(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Style Sheet Example_001")
self.setMinimumSize(600,800)
self.setMaximumSize(600,800)
self.mydesign()
def mydesign(self):
lbl_normal = QLabel("Total Available Items :")
lbl_starts = QLabel("Item Starts With :")
lbl_contains = QLabel("Items Contains Anywhere :")
lbl_end = QLabel("Items Ends With :")
lbl_normal_count = QLabel("500")
lbl_starts_count = QLabel('50,000')
lbl_contains_count = QLabel('5')
lbl_ends_count = QLabel('0')
mylayout = QFormLayout()
mylayout.addRow(lbl_normal ,lbl_normal_count)
mylayout.addRow(lbl_starts ,lbl_starts_count)
mylayout.addRow(lbl_contains,lbl_contains_count)
mylayout.addRow(lbl_end ,lbl_ends_count)
self.setLayout(mylayout)
def main():
myapp = QApplication(sys.argv)
mywindow = Stylesheet_001()
mywindow.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
1) lbl_normal, 2) lbl_strats, 3) lbl_contains, 4) lbl_ends belong to one group (First) and another 4 Labels belong to another group (second group).
First group have to set same, fore-ground color:Red,font: Caliber,10,Bold and Alignment to Right. Second group have to set same, fore-ground colour:Green,font:system default and Alignment to Left.
Try it:
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QFormLayout,\
QFrame, QVBoxLayout
from PyQt5.QtCore import Qt
class Stylesheet_001(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Style Sheet Example_001")
self.setMinimumSize(600,800)
self.setMaximumSize(600,800)
self.mydesign()
def mydesign(self):
# Red, same font : Caliber, 10, Bold, and Alignment to Right
lbl_normal = QLabel("Total Available Items :", alignment=Qt.AlignRight | Qt.AlignVCenter)
lbl_normal.setMinimumWidth(180)
lbl_starts = QLabel("Item Starts With :", minimumWidth=180, alignment=Qt.AlignRight | Qt.AlignVCenter)
lbl_contains = QLabel("Items Contains Anywhere :", minimumWidth=180, alignment=Qt.AlignRight | Qt.AlignVCenter)
lbl_end = QLabel("Items Ends With :", minimumWidth=180, alignment=Qt.AlignRight | Qt.AlignVCenter)
lbl_normal_count = QLabel("500")
lbl_starts_count = QLabel('50,000')
lbl_contains_count = QLabel('5')
lbl_ends_count = QLabel('0')
mylayout = QFormLayout(self)
mylayout.addRow(lbl_normal , lbl_normal_count)
mylayout.addRow(lbl_starts , lbl_starts_count)
mylayout.addRow(lbl_contains, lbl_contains_count)
mylayout.addRow(lbl_end , lbl_ends_count)
QSS = """
QWidget > QLabel[text="Total Available Items :"],
QWidget > QLabel[text="Item Starts With :"],
QWidget > QLabel[text="Items Contains Anywhere :"],
QWidget > QLabel[text="Items Ends With :"] {
background-color: red;
color: white;
font: bold 10pt \"Caliber\";
}
QWidget > QLabel {
background-color: green;
color: yellow;
font: 10pt \"Caliber\";
}
"""
def main():
myapp = QApplication(sys.argv)
myapp.setStyle('Fusion')
myapp.setStyleSheet(QSS)
mywindow = Stylesheet_001()
mywindow.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
Try it:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Stylesheet_001(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Style Sheet Example_001")
self.setMinimumSize(600,800)
# self.setMaximumSize(600,800)
self.mydesign()
def mydesign(self):
lbl_normal = QLabel("Total Available Items :")
lbl_starts = QLabel("Item Starts With :")
lbl_contains = QLabel("Items Contains Anywhere :")
lbl_end = QLabel("Items Ends With :")
lbl_normal.setStyleSheet(self.stylesheet_family_1())
lbl_starts.setStyleSheet(self.stylesheet_family_1())
lbl_contains.setStyleSheet(self.stylesheet_family_1())
lbl_end.setStyleSheet(self.stylesheet_family_1())
lbl_normal_count = QLabel("500")
lbl_starts_count = QLabel('50,000')
lbl_contains_count = QLabel('5')
lbl_ends_count = QLabel('0')
lbl_normal_count.setStyleSheet(self.stylesheet_family_2())
lbl_starts_count.setStyleSheet(self.stylesheet_family_2())
lbl_contains_count.setStyleSheet(self.stylesheet_family_2())
lbl_ends_count.setStyleSheet(self.stylesheet_family_2())
self.mylayout = QFormLayout()
self.mylayout.setSpacing(0)
self.mylayout.addRow(lbl_normal ,lbl_normal_count)
self.mylayout.addRow(lbl_starts ,lbl_starts_count)
self.mylayout.addRow(lbl_contains,lbl_contains_count)
self.mylayout.addRow(lbl_end ,lbl_ends_count)
self.myframe = QFrame()
self.mainlayout = QVBoxLayout()
self.myframe.setFixedSize(400,300)
self.myframe.setLayout(self.mylayout)
self.mainlayout.addWidget(self.myframe)
self.mainlayout.addStretch()
self.setLayout(self.mainlayout)
def stylesheet_family_1(self):
return"""
QLabel{
background-color:'red';
font-family: Caliber;
font-style: normal;
font-size:10pt;
font-weight:normal;
border-width:1px;
border-color:black;
border-top-color:black;
border-style:outset;
color:white;
padding:6px;
min-width:220px;
qproperty-alignment:'AlignRight';
}
"""
def stylesheet_family_2(self):
return"""
QLabel{
background-color:'green';
font-family: Caliber;
font-style: normal;
font-size:10pt;
font-weight:bold;
border-width:1px;
border-color:black;
border-top-color:black;
border-style:outset;
color:white;
padding:1px;
min-width:20px;
qproperty-alignment:'AlignCenter|AlignRight';
}
"""
def main():
myapp = QApplication(sys.argv)
mywindow = Stylesheet_001()
mywindow.show()
sys.exit(myapp.exec_())
if __name__ == "__main__":
main()
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.