Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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 在不丢失原始样式的情况下更新样式表_Python_Pyqt_Pyqt5_Qtstylesheets - Fatal编程技术网

Python 在不丢失原始样式的情况下更新样式表

Python 在不丢失原始样式的情况下更新样式表,python,pyqt,pyqt5,qtstylesheets,Python,Pyqt,Pyqt5,Qtstylesheets,我正在创建具有特定样式的QpushButton,然后更新样式表,以便稍后对其进行着色。但是,当我这样做时,它会覆盖原始样式。有没有一种方法可以更新或附加到对象的样式表,而不丢失它或重新定义所有内容 import sys from PyQt5 import QtCore, QtWidgets from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout,QPushButton, QMenu, QApplication, QWidget,

我正在创建具有特定样式的QpushButton,然后更新样式表,以便稍后对其进行着色。但是,当我这样做时,它会覆盖原始样式。有没有一种方法可以更新或附加到对象的样式表,而不丢失它或重新定义所有内容

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout,QPushButton, QMenu, QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtCore import QSize, QCoreApplication, Qt, QLine, QPoint
from PyQt5.QtGui import QPainter, QPen, QFont,QPainter, QPainterPath, QPixmap


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setMinimumSize(QSize(400, 400))   

        color = QPushButton('color', self)
        color.clicked.connect(self.color)
        color.resize(100,115)
        color.move(0, 100)

        buttons= ['button1','button2','button3']
        self.pybutton = {}
        x=0
        t = 0
        for i in buttons:
            t = t + 100
            x+=1
            self.pybutton[str(x)] = QPushButton(i, self) 
            self.pybutton[str(x)].setObjectName('btn' + str(x))
            self.pybutton[str(x)].resize(100,100)
            self.pybutton[str(x)].move(400-int(t),100)
            self.pybutton[str(x)].setStyleSheet('QPushButton {max-width: 75px;text-align:center;padding-left: 20px; max-height: 60px; font-size: 20px;}')
        self.statusBar()
    def status(self):
        sender = self.sender()
        print('PyQt5 button click')
        self.statusBar().showMessage(sender.text() + ' was pressed')

    def color(self):
        for i in self.pybutton:
            self.pybutton[str(i)].objectName()
            if self.pybutton[str(i)].objectName() == 'btn1':
                self.pybutton[str(i)].setStyleSheet("background-color: green")
            else:
                self.pybutton[str(i)].setStyleSheet("background-color: red")    

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

一个可能的解决方案是使用
stylesheet()
方法设置样式表,然后分析文本并根据需要修改它,但这可能非常困难。另一个更好的解决方案是使用,并通过修改属性来选择颜色:

import sys
from PyQt5.QtWidgets import QMainWindow,QPushButton, QApplication
from PyQt5.QtCore import QSize


class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setMinimumSize(QSize(400, 400))   

        color = QPushButton('color', self)
        color.clicked.connect(self.color)
        color.resize(100,115)
        color.move(0, 100)

        buttons= ['button1','button2','button3']
        self.pybutton = {}

        qss = """
        QPushButton{
            max-width: 75px;
            text-align:center;
            padding-left: 20px; 
            max-height: 60px; 
            font-size: 20px;
        }
        QPushButton[color = "0"]{
            background-color: green;
        }
        QPushButton[color = "1"]{
            background-color: red;
        }
        """

        for i, text in enumerate(buttons):
            btn = QPushButton(text, self) 
            btn.setObjectName('btn{}'.format(i))
            btn.setGeometry(300-i*100, 100, 100,100)
            btn.setStyleSheet(qss)
            self.pybutton[str(i)] = btn

    def color(self):
        for i, btn in self.pybutton.items():
            if btn.objectName() == 'btn1':
                btn.setProperty("color", "0")
            else:
                btn.setProperty("color", "1") 
            btn.style().polish(btn)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWin = MainWindow()
    mainWin.show()
    sys.exit( app.exec_() )

如果您不知道所有其他参数,因为它们使用默认调色板,该怎么办?