首页 > 解决方案 > PyQt5 设置样式表错误

问题描述

这是我的来源,但是当我单击一个按钮并在另一个按钮之后,颜色不会改变,它将保持最后一种颜色

from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(702, 557)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(110, 80, 101, 81))
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.red)
        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
        self.pushButton_2.setGeometry(QtCore.QRect(300, 80, 101, 81))
        self.pushButton_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton.clicked.connect(self.blue)
        self.pushButton_3 = QtWidgets.QPushButton(Dialog)
        self.pushButton_3.setGeometry(QtCore.QRect(480, 80, 101, 81))
        self.pushButton_3.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton.clicked.connect(self.yellow)
        self.pushButton_4 = QtWidgets.QPushButton(Dialog)
        self.pushButton_4.setGeometry(QtCore.QRect(110, 220, 101, 81))
        self.pushButton_4.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton.clicked.connect(self.green)
        self.pushButton_5 = QtWidgets.QPushButton(Dialog)
        self.pushButton_5.setGeometry(QtCore.QRect(300, 220, 101, 81))
        self.pushButton_5.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_5.setObjectName("pushButton_5")
        self.pushButton.clicked.connect(self.black)
        self.pushButton_6 = QtWidgets.QPushButton(Dialog)
        self.pushButton_6.setGeometry(QtCore.QRect(480, 220, 101, 81))
        self.pushButton_6.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_6.setObjectName("pushButton_6")
        self.pushButton.clicked.connect(self.white)
        self.pushButton_7 = QtWidgets.QPushButton(Dialog)
        self.pushButton_7.setGeometry(QtCore.QRect(110, 360, 101, 81))
        self.pushButton_7.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_7.setObjectName("pushButton_7")
        self.pushButton.clicked.connect(self.purple)
        self.pushButton_8 = QtWidgets.QPushButton(Dialog)
        self.pushButton_8.setGeometry(QtCore.QRect(300, 360, 101, 81))
        self.pushButton_8.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_8.setObjectName("pushButton_8")
        self.pushButton.clicked.connect(self.orange)
        self.pushButton_9 = QtWidgets.QPushButton(Dialog)
        self.pushButton_9.setGeometry(QtCore.QRect(480, 360, 101, 81))
        self.pushButton_9.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.pushButton_9.setObjectName("pushButton_9")
        self.pushButton.clicked.connect(self.pink)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "red"))
        self.pushButton_2.setText(_translate("Dialog", "blue"))
        self.pushButton_3.setText(_translate("Dialog", "yellow"))
        self.pushButton_4.setText(_translate("Dialog", "green"))
        self.pushButton_5.setText(_translate("Dialog", "black"))
        self.pushButton_6.setText(_translate("Dialog", "white"))
        self.pushButton_7.setText(_translate("Dialog", "purple"))
        self.pushButton_8.setText(_translate("Dialog", "orange"))
        self.pushButton_9.setText(_translate("Dialog", "pink"))
    def red(self):
        Dialog.setStyleSheet("background-color: red;")
    def blue(self):
        Dialog.setStyleSheet("background-color: blue;")
    def yellow(self):
        Dialog.setStyleSheet("background-color: yellow;")
    def green(self):
        Dialog.setStyleSheet("background-color: green;")
    def black(self):
        Dialog.setStyleSheet("background-color: black;")
    def white(self):
        Dialog.setStyleSheet("background-color: white;")
    def purple(self):
        Dialog.setStyleSheet("background-color: purple")
    def orange(self):
        Dialog.setStyleSheet("background-color: orange;")
    def pink(self):
        Dialog.setStyleSheet("background-color: pink;")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

当单击另一个按钮时,它不会改变颜色

在此处输入图像描述

标签: pythonpython-3.xqtpyqtpyqt5

解决方案


OP 的 MCVE 对我来说似乎过于复杂。因此,我首先通过自己的尝试开始对此进行调查:

testStyleSheet.py

#!/usr/bin/python3

import sys
from PyQt5.QtCore import QT_VERSION_STR
from PyQt5.QtWidgets import QApplication, QDialog, QWidget
from PyQt5.QtWidgets import QGridLayout, QPushButton

print("Qt Version: {}".format(QT_VERSION_STR))
app = QApplication(sys.argv)
qDlg = QDialog()
qGrid = QGridLayout()
configs = [ \
  [ "red", 0, 0 ], [ "green", 0, 1 ], [ "blue", 0, 2 ], \
  [ "cyan", 1, 0 ], [ "yellow", 1, 1 ], [ "magenta", 1, 2], \
  [ "pink", 2, 0 ], [ "brown", 2, 1 ], [ "gray", 2, 2 ] \
]
for config in configs:
  color = config[0]
  qBtn = QPushButton(color)
  qGrid.addWidget(qBtn, config[1], config[2]) 
  def makeLambda(color):
    return lambda state: \
      qDlg.setStyleSheet("background-color: {};".format(color))
  qBtn.clicked.connect(makeLambda(color))
qDlg.setLayout(qGrid)
qDlg.show()
sys.exit(app.exec_())

输出:

Qt Version: 5.11.3

带有演示会话的 GIF 动画

好的,它的工作一般。


仔细查看 OPs 代码,我最终发现了为什么它不能按预期工作:

OP 将所有信号槽连接到self.push_button

        self.pushButton.clicked.connect(self.red)
...
        self.pushButton.clicked.connect(self.blue)
...
        self.pushButton.clicked.connect(self.yellow)
...
        self.pushButton.clicked.connect(self.green)
...
        self.pushButton.clicked.connect(self.black)
...
        self.pushButton.clicked.connect(self.white)
...
        self.pushButton.clicked.connect(self.purple)
...
        self.pushButton.clicked.connect(self.orange)
...
        self.pushButton.clicked.connect(self.pink)

这实际上看起来像一个简单的错字,但它可能会被忽略,因为它隐藏在代码墙中非常好。(我必须承认,直到非常接近代码时我才看到这一点。)

因此,如果self.push_button单击,则此按钮会调用所有九个插槽,这pink是其中的最后一个。(因此,其他人的影响只是不可见。)

如果单击任何其他按钮,则不会调用任何内容。

解决此问题后,OPs 示例也可以正常工作。


似乎 OP 打算通过按名称连接插槽来解决此问题:
OPs 代码中也有自动连接(可能由 Qt-s GUI 构建器的代码生成器插入):

        QtCore.QMetaObject.connectSlotsByName(Dialog)

我很确定这部分也不能正常工作。我记得我曾经研究过自动连接的工作原理。(并不是说我曾经打算使用它。它对我来说似乎太脆弱了,我更喜欢维护友好的解决方案(但这只是我个人的看法)。然而,我曾经出于好奇对此进行过调查……)

pyqt自动连接信号

回想一下,我得出的结论是,没有一个工作方案与 OP 代码匹配。


推荐阅读