簡體   English   中英

PyQt5 - 如何為相同的元素設置不同的樣式表?

[英]PyQt5 - How can I set different stylesheets for the same elements?

我的部分代碼是:

  layout2 = QGridLayout()

  #Design
  self.tab2.setStyleSheet("""
     .QLabel {
        color: white;
        font-weight: bold;
        border: 0px;
        margin: 0px;
        padding: 0px;
     }

     .QWidget {
        background-color: rgb(0,0,0);
        }
    """)

[...]

  layout2.addWidget(QLabel("1"),1,0)
  layout2.addWidget(QLabel("SHIRT"),1,1)
  #Yahoo details:
  layout2.addWidget(QLabel("Your-email"),1,2)
  layout2.addWidget(QLabel(":"),1,3)      
  layout2.addWidget(QLabel("Your-pass"),1,4)
  #-----
  layout2.addWidget(QLabel("Status"),1,8)
  layout2.addWidget(QLabel("Action"),1,9)

我正在嘗試在我擁有的 QLabel 元素上設置不同的顏色(以及其他一些東西,文本對齊等)

我怎么能單獨做到這一點? 這樣我就不會改變所有這些???

請幫忙,謝謝!

您必須使用對象名稱:

from PyQt5 import QtWidgets

QSS = '''

.QLabel {
    color: white;
    font-weight: bold;
    border: 0px;
    margin: 0px;
    padding: 0px;
}

.QWidget {
    background-color: rgb(0,0,0);
}

.QLabel#1 {
    background-color: blue; 
}

.QLabel#type1 {
    background-color: yellow;   
}
.QLabel#email {
    color: red;
}
.QLabel#password {
    background-color: green;    
    qproperty-alignment: 'AlignVCenter | AlignRight';
}
'''

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    w.setStyleSheet(QSS)
    layout = QtWidgets.QGridLayout(w)
    layout.addWidget(QtWidgets.QLabel("1", objectName="type1"),1,0)
    layout.addWidget(QtWidgets.QLabel("SHIRT", objectName="shirt"),1,1)

    #Yahoo details:
    layout.addWidget(QtWidgets.QLabel("Your-email", objectName="email"),1,2)
    layout.addWidget(QtWidgets.QLabel(":"),1,3)      
    layout.addWidget(QtWidgets.QLabel("Your-pass", objectName="password"),1,4)
    #-----
    layout.addWidget(QtWidgets.QLabel("Status", objectName="type1"),1,8)
    layout.addWidget(QtWidgets.QLabel("Action"),1,9)
    w.show()
    sys.exit(app.exec_())

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM