1

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?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
RealRK
  • 315
  • 3
  • 19
  • I don't understand you, explain yourself better, besides providing a [MRE] Do you want to restore the palette to the default value? – eyllanesc Feb 21 '20 at 18:29
  • @eyllanesc Yes, I want to restore to default. But I think what I read is deprecated. Any hints/pointers are hugely appreaciated. Have updated the code so it can run. Sorry for the large code, I used pyuic5. – RealRK Feb 21 '20 at 18:39
  • 1
    @eyllanesc Just updated correct code – RealRK Feb 21 '20 at 18:43
  • You could just change both `app.setPalette()` calls to `Form.setPalette()` – alec Feb 21 '20 at 18:52

1 Answers1

5

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • @In-Vader Are you sure? Have you tested my code without modifying it? I just tested it and it works correctly. what are your OS, python version, pyqt5 version? – eyllanesc Feb 21 '20 at 19:02
  • 1
    Yeah, I'm on python 3.7.5, pyqt5, windows 10. Its showing the bar, but not changing anything. Sorry for pester – RealRK Feb 21 '20 at 19:08
  • @In-Vader What version of PyQt5 do you use? I have tested it on Linux with Python 3.8.1 and PyQt5 5.14.1 and it works correctly. – eyllanesc Feb 21 '20 at 19:13
  • 1
    @In-Vader change `QtGui.QGuiApplication.setPalette(darkpalette)` to `self.setPalette(darkpalette)` and `QtGui.QGuiApplication.setPalette(self.default_palette)` to `self.setPalette(self.default_palette)` – S. Nick Feb 21 '20 at 19:31
  • 2
    @S.Nick that did the trick! Thanks both of you. Also, I ran the code posted by eyllanesc and it works on python 3.8.1 on linux vm but not on 3.7.5, that's weird tbh – RealRK Feb 21 '20 at 19:32
  • If colors of the window are set with `QtDesigner` in `setStyleSheet`, the palette will not be overwritten by `self.setPalette(darkpalette)` – Papageno Mar 08 '21 at 16:06