pythonpython-3.xpyqtpyqt5qpalette

Change palette of PyQt5


Am attempting to change the GUI palette from dark to light.

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_())

This is how PyQt5 says the palette can be set to default (changeSkinLight method).

But when I assign the functions to an action. It just runs the changeDarkSkin and does nothing on changeLightSkin. How can I set the palette to default like it was in QtDesigner?


Solution

  • You have to save the QPalette by default if you want to restore the application palette:

    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_())

    Note:

    There seems to be a bug for certain versions of PyQt5 and python so a workaround is setting the palette to the window(thanks @S.Nick):

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