在Pyside + Python中使用类进行样式化 [英] Styling with classes in Pyside + Python

查看:333
本文介绍了在Pyside + Python中使用类进行样式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更好地使用类来设计这个应用程序,而不是为每个应该看起来一样的标签重新定义相同的样式。这使得改变样式变得很痛苦,因为我必须通过每个看起来都一样的标签并粘贴代码来匹配。

 #!/ usr / bin / python 
# - * - 编码:utf-8 - * -

import sys
从PySide导入QtGui,QtCore

class Example(QtGui.QWidget):

def __init __(self):
super(Example,self).__ init __()

self.initUI()

def initUI(self):

#将所有GUI元素添加到类
self.amountLabel = QtGui.QLabel ')
self.counterLabel = QtGui.QLabel('Counter')
self.totalLabel = QtGui.QLabel('Total')
self.amountSpin = QtGui.QSpinBox b self.counterSpin = QtGui.QSpinBox()

self.totalOutput = QtGui.QLabel('0')

grid = QtGui.QGridLayout()
grid .setSpacing(0)

grid.addWidget(self.amountLabel,3,0)
grid.addWidget(self.counterLabel,3,1)
grid.addWidget .totalLabel,3,2)

grid.addWidget(self.amountSpin,4,0)
grid.addWidget(self.counterSpin,4,1)
grid.addWidget (self.totalOutput,4,2)

self.setLayout(grid)

#STYLING
self.amountLabel.setStyleSheet(QLabel {color:rgb 50,50,50); font-size:11px; background-color:rgba(188,188,188,50); border:1px solid rgba(188,188,188,250); })
self.amountSpin.setStyleSheet(QSpinBox {color:rgb(50,50,50); font-size:11px; background-color:rgba(255,188,20,50);} )

self.counterLabel.setStyleSheet(QLabel {color:rgb(50,50,50); font-size:11px; background-color:rgba(188,188,188,50) border:1px solid rgba(188,188,188,250);})
self.counterSpin.setStyleSheet(QSpinBox {color:rgb(50,50,50); font-size:11px; color:rgba(255,188,20,50);})

self.totalLabel.setStyleSheet(QLabel {color:rgb(50,50,50); font-weight:bold; font-size:11px; background-color:rgba(26,188,182,150); border:1px solid rgba(26,188,182,255);})
self.totalOutput.setStyleSheet QLabel {color:rgb(50,50,50); font-weight:bold; font-size:11px; background-color:rgba(26,188,182,50); border:1px solid rgba(26, 182,255);})

self.setGeometry(800,400,250,80)
self.setWindowTitle('Simple Calculator')
self.show

def main():

app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app。 exec_())


如果__name__ =='__main__':
main()

$如果我没有错误,父母的样式表将应用于所有的孩子,除非被覆盖。



您可以尝试:

 #STYLING 
self.setStyleSheet(QLabel {color:rgb ,50,50); font-size:11px; background-color:rgba(188,188,188,50); border:1px solid rgba(188,188,188,250); } QSpinBox {color:rgb(50,50,50); font-size:11px; background-color:rgba(255,188,20,50); })

#setting自己的CS(widget)而不是子项

self.setGeometry(800,400,250,80)
self。 setWindowTitle('Simple Calculator')
self.show()

  label1.setProperty('labelClass','red')
label2.setProperty('labelClass','blue ')

和小部件的CS:

  QLabel [labelClass =red] {...} 
QLabel [labelClass =blue] {...}


How can I better style this app using classes rather than redefining the same styles for every label that should look the same. It makes it a pain to change a style because I then have to go through every label that is suppose to look the same and paste the code to match.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        #Add all GUI Elements to Class
        self.amountLabel = QtGui.QLabel('Amount')
        self.counterLabel = QtGui.QLabel('Counter')
        self.totalLabel = QtGui.QLabel('Total')
        self.amountSpin = QtGui.QSpinBox()
        self.counterSpin = QtGui.QSpinBox()

        self.totalOutput = QtGui.QLabel('0')

        grid = QtGui.QGridLayout()
        grid.setSpacing(0)

        grid.addWidget(self.amountLabel, 3, 0)
        grid.addWidget(self.counterLabel, 3, 1)
        grid.addWidget(self.totalLabel, 3, 2)

        grid.addWidget(self.amountSpin, 4, 0)
        grid.addWidget(self.counterSpin, 4, 1)
        grid.addWidget(self.totalOutput, 4, 2)

        self.setLayout(grid)

        # STYLING
        self.amountLabel.setStyleSheet("QLabel { color: rgb(50, 50, 50); font-size: 11px; background-color: rgba(188, 188, 188, 50); border: 1px solid rgba(188, 188, 188, 250); }")
        self.amountSpin.setStyleSheet("QSpinBox { color: rgb(50, 50, 50); font-size: 11px; background-color: rgba(255, 188, 20, 50); }")

        self.counterLabel.setStyleSheet("QLabel { color: rgb(50, 50, 50); font-size: 11px; background-color: rgba(188, 188, 188, 50); border: 1px solid rgba(188, 188, 188, 250); }")
        self.counterSpin.setStyleSheet("QSpinBox { color: rgb(50, 50, 50); font-size: 11px; background-color: rgba(255, 188, 20, 50); }")

        self.totalLabel.setStyleSheet("QLabel { color: rgb(50, 50, 50); font-weight:bold; font-size: 11px; background-color: rgba(26, 188, 182, 150); border: 1px solid rgba(26, 188, 182, 255); }")
        self.totalOutput.setStyleSheet("QLabel { color: rgb(50, 50, 50); font-weight:bold; font-size: 11px; background-color: rgba(26, 188, 182, 50); border: 1px solid rgba(26, 188, 182, 255); }")

        self.setGeometry(800, 400, 250, 80)
        self.setWindowTitle('Simple Calculator')
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

解决方案

If I am not mistaken, stylesheets of a parent are applied to all children unless overwritten.

You can try this:

    # STYLING
    self.setStyleSheet("QLabel { color: rgb(50, 50, 50); font-size: 11px; background-color: rgba(188, 188, 188, 50); border: 1px solid rgba(188, 188, 188, 250); } QSpinBox { color: rgb(50, 50, 50); font-size: 11px; background-color: rgba(255, 188, 20, 50); }")

    #setting CS of self (the widget) and not the children

    self.setGeometry(800, 400, 250, 80)
    self.setWindowTitle('Simple Calculator')
    self.show()

Concerning your different buttons:

label1.setProperty('labelClass', 'red')
label2.setProperty('labelClass', 'blue')

and in the CS of the widget:

QLabel[labelClass="red"] {...}
QLabel[labelClass="blue"] {...}

这篇关于在Pyside + Python中使用类进行样式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆