Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过PyQt5中的变量添加颜色、背景色值?_Python_Pyqt_Pyqt5 - Fatal编程技术网

Python 如何通过PyQt5中的变量添加颜色、背景色值?

Python 如何通过PyQt5中的变量添加颜色、背景色值?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,这是我的密码。如何通过变量为CSS赋值,并在PyQt5样式表中使用相同的值 self.label = QLabel("sample") self.label.setObjectName("label_1") self.label.setStyleSheet(my_stylesheet()) def my_stylesheet(): return """ QLabel#label_1{c

这是我的密码。如何通过变量为CSS赋值,并在PyQt5样式表中使用相同的值

    self.label = QLabel("sample")
    self.label.setObjectName("label_1")
    self.label.setStyleSheet(my_stylesheet())

def my_stylesheet():
    return """
    QLabel#label_1{color:red;background-color:blue;}
    """
不要直接赋值(如红色、蓝色),而是在变量中指定它以及如何使用它。 例如:


一种可能的解决方案是在setter中应用Qt样式表更改时使用属性:

导入系统 从PyQt5.QtCore导入Qt 从PyQt5.QtGui导入QColor,QFont 从PyQt5.QtWidgets导入QApplication、QLabel、QMainWindow 类标签(QLabel): 定义初始化__( 自身,背景=QColor(“白色”),前景=QColor(“黑色”),父项=None ): super()。\uuuu init\uuuu(父级) 自身背景=背景 自我。前景=前景 self.\u change\u样式表() @财产 def背景(自我): 返回自己的背景 @背景设置器 def背景(自身、颜色): 如果self.\u background==颜色: 返回 self.\u背景=颜色 self.\u change\u样式表() @财产 def前景(自我): 返回自我 @前景设定器 def前景(自身、颜色): 如果self.\u前景==颜色: 返回 self.\u前景=颜色 self.\u change\u样式表() def_change_样式表(self): qss=“QLabel{color:%s;背景色:%s}”%( self.background.name(), self.foreground.name(), ) 自我设置样式表(qss) 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) label=label() label.setText(“Qt太棒了!!!”) label.setAlignment(Qt.AlignCenter) label.setFont(QFont(“Arial”,40)) label.background=QColor(“红色”) label.foreground=QColor(“蓝色”) w=QMainWindow() w、 setCentralWidget(标签) w、 调整大小(640480) w、 show() sys.exit(app.exec_())
此代码不能直接解决您的问题。但它解决了你的问题

import sys
from PyQt5 import QtWidgets

color_red = "color_red"
color_blue = "color_blue"

backcolor_skyblue = "bcolor_skyblue"
backcolor_lightgreen = "bcolor_lightgreen"

class CssSample(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Css Stylesheet")

        self.label_1 = QtWidgets.QLabel("Sample 1 Label")
        self.label_1.setFixedSize(200,50)
        self.label_1.setProperty("color",color_red )
        self.label_1.setProperty("backcolor",backcolor_skyblue)
        self.label_1.setStyleSheet(my_stylesheet())

        self.label_2 = QtWidgets.QLabel("Sample 2 Label")
        self.label_2.setFixedSize(200, 50)
        self.label_2.setProperty("color", color_blue)
        self.label_2.setProperty("backcolor",backcolor_lightgreen)
        self.label_2.setStyleSheet(my_stylesheet())

        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.addWidget(self.label_1)
        self.vbox.addWidget(self.label_2)
        self.setLayout(self.vbox)

def my_stylesheet():
    
    return """
    QLabel[color = "color_red"]
    { color : red;font-family: Trebuchet MS; font-style: normal; font-size:12pt; font-weight:700; }
    
    QLabel[color = "color_blue"]
    { color : blue;font-family: Trebuchet MS; font-style: normal; font-size:12pt; font-weight:700;}
    
    QLabel[backcolor = "bcolor_skyblue"]
    { background-color : skyblue}
    
    QLabel[backcolor = "bcolor_lightgreen"]
    { background-color : lightgreen}
    
    QLabel{ background-color: @mycolor;}
    
"""

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainwindow = CssSample()
    mainwindow.show()
    sys.exit(app.exec_())

使用f-string或
str.format
方法,例如:
f“{{color:{color\u 1};背景色:{color\u 2};}}”
不适用于meDid您使用这个吗
f“QLabel#label_1{{{color:{color_1};background color:{color_2};}}}”
color_1=red
中,红色应该是字符串。@tckrauomuqnt是否可以在样式表中使用“动态”变量引用,以便每次该变量的值更改时样式表也会更新?如果是这样的话,那就不行了:样式表是静态字符串,一旦设置好,就会进行计算。@Bala
import sys
from PyQt5 import QtWidgets

color_red = "color_red"
color_blue = "color_blue"

backcolor_skyblue = "bcolor_skyblue"
backcolor_lightgreen = "bcolor_lightgreen"

class CssSample(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Css Stylesheet")

        self.label_1 = QtWidgets.QLabel("Sample 1 Label")
        self.label_1.setFixedSize(200,50)
        self.label_1.setProperty("color",color_red )
        self.label_1.setProperty("backcolor",backcolor_skyblue)
        self.label_1.setStyleSheet(my_stylesheet())

        self.label_2 = QtWidgets.QLabel("Sample 2 Label")
        self.label_2.setFixedSize(200, 50)
        self.label_2.setProperty("color", color_blue)
        self.label_2.setProperty("backcolor",backcolor_lightgreen)
        self.label_2.setStyleSheet(my_stylesheet())

        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.addWidget(self.label_1)
        self.vbox.addWidget(self.label_2)
        self.setLayout(self.vbox)

def my_stylesheet():
    
    return """
    QLabel[color = "color_red"]
    { color : red;font-family: Trebuchet MS; font-style: normal; font-size:12pt; font-weight:700; }
    
    QLabel[color = "color_blue"]
    { color : blue;font-family: Trebuchet MS; font-style: normal; font-size:12pt; font-weight:700;}
    
    QLabel[backcolor = "bcolor_skyblue"]
    { background-color : skyblue}
    
    QLabel[backcolor = "bcolor_lightgreen"]
    { background-color : lightgreen}
    
    QLabel{ background-color: @mycolor;}
    
"""

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainwindow = CssSample()
    mainwindow.show()
    sys.exit(app.exec_())