1

我正在尝试将 GUI 调色板从深色更改为浅色。

from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
from PyQt5.QtCore import *


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.gridLayout = QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.pushButton_2 = QPushButton(Form)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)

        self.retranslateUi(Form)
        QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "Dark"))
        self.pushButton_2.setText(_translate("Form", "Light"))
        self.pushButton.clicked.connect(self.changeSkinDark)
        self.pushButton_2.clicked.connect(self.changeSkinLight)

    def changeSkinDark(self):

        darkpalette = QPalette()
        darkpalette.setColor(QPalette.Window, QColor(41,44,51))
        darkpalette.setColor(QPalette.WindowText, Qt.white)
        darkpalette.setColor(QPalette.Base, QColor(15,15,15))
        darkpalette.setColor(QPalette.AlternateBase, QColor(41,44,51))
        darkpalette.setColor(QPalette.ToolTipBase, Qt.white)
        darkpalette.setColor(QPalette.ToolTipText, Qt.white)
        darkpalette.setColor(QPalette.Text, Qt.white)
        darkpalette.setColor(QPalette.Button, QColor(41,44,51))
        darkpalette.setColor(QPalette.ButtonText, Qt.white)
        darkpalette.setColor(QPalette.BrightText, Qt.red)
        darkpalette.setColor(QPalette.Highlight, QColor(100,100,225))
        darkpalette.setColor(QPalette.HighlightedText, Qt.black)
        app.setPalette(darkpalette)

    def changeSkinLight(self):

        palette = QGuiApplication.palette()
        app.setPalette(palette)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    app.setStyle('Fusion')
    Form = QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

这就是 PyQt5 所说的调色板可以设置为默认值(changeSkinLight 方法)的方式。

但是当我将功能分配给一个动作时。它只是运行 changeDarkSkin 而对 changeLightSkin 不做任何事情。如何像在 QtDesigner 中一样将调色板设置为默认值?

4

1 回答 1

4

如果要恢复应用程序调色板,则必须默认保存 QPalette:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.gridLayout = QtWidgets.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)

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

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.pushButton.setText(_translate("Form", "Dark"))
        self.pushButton_2.setText(_translate("Form", "Light"))


class Form(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.changeSkinDark)
        self.pushButton_2.clicked.connect(self.changeSkinLight)
        self.default_palette = QtGui.QGuiApplication.palette()

    @QtCore.pyqtSlot()
    def changeSkinDark(self):
        darkpalette = QtGui.QPalette()
        darkpalette.setColor(QtGui.QPalette.Window, QtGui.QColor(41, 44, 51))
        darkpalette.setColor(QtGui.QPalette.WindowText, QtCore.Qt.white)
        darkpalette.setColor(QtGui.QPalette.Base, QtGui.QColor(15, 15, 15))
        darkpalette.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor(41, 44, 51))
        darkpalette.setColor(QtGui.QPalette.ToolTipBase, QtCore.Qt.white)
        darkpalette.setColor(QtGui.QPalette.ToolTipText, QtCore.Qt.white)
        darkpalette.setColor(QtGui.QPalette.Text, QtCore.Qt.white)
        darkpalette.setColor(QtGui.QPalette.Button, QtGui.QColor(41, 44, 51))
        darkpalette.setColor(QtGui.QPalette.ButtonText, QtCore.Qt.white)
        darkpalette.setColor(QtGui.QPalette.BrightText, QtCore.Qt.red)
        darkpalette.setColor(QtGui.QPalette.Highlight, QtGui.QColor(100, 100, 225))
        darkpalette.setColor(QtGui.QPalette.HighlightedText, QtCore.Qt.black)
        QtGui.QGuiApplication.setPalette(darkpalette)

    @QtCore.pyqtSlot()
    def changeSkinLight(self):
        QtGui.QGuiApplication.setPalette(self.default_palette)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Form()
    w.show()
    sys.exit(app.exec_())

笔记:

PyQt5 和 python 的某些版本似乎存在错误,因此解决方法是将调色板设置为窗口(感谢@S.Nick):

@QtCore.pyqtSlot()
def changeSkinDark(self):
    # ...
    self.setPalette(darkpalette)

@QtCore.pyqtSlot()
def changeSkinLight(self):
    self.setPalette(self.default_palette)
于 2020-02-21T18:49:31.347 回答