Trying to make a dark- lightmode switcher

Question:

I’m trying to make a Button which switches between light and dark mode. Switching to dark mode is pretty easy, but switching to light mode again is a bit complicated.

I am trying to switch to the default Palette but I don’t know how it really works.

This is the function I’m using:

    def modeSwitch(self):
        if self.dark_mode:
            self.dark_mode = False
            QApplication.setPalette("")
        else:   
            self.dark_mode = True
            dark_palette = QPalette()
            dark_palette.setColor(QPalette.Window, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.WindowText, Qt.white)
            dark_palette.setColor(QPalette.Base, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.AlternateBase, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.ToolTipBase, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.ToolTipText, Qt.white)
            dark_palette.setColor(QPalette.Text, Qt.white)
            dark_palette.setColor(QPalette.Button, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.ButtonText, Qt.white)
            dark_palette.setColor(QPalette.BrightText, Qt.red)
            dark_palette.setColor(QPalette.Link, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.Highlight, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.HighlightedText, QColor(97, 97, 97))
            dark_palette.setColor(QPalette.Active, QPalette.Button, QColor(35, 35, 35))
            dark_palette.setColor(QPalette.Disabled, QPalette.ButtonText, Qt.darkGray)
            dark_palette.setColor(QPalette.Disabled, QPalette.WindowText, Qt.darkGray)
            dark_palette.setColor(QPalette.Disabled, QPalette.Text, Qt.darkGray)
            dark_palette.setColor(QPalette.Disabled, QPalette.Light, QColor(35, 35, 35))
            QApplication.setPalette(dark_palette)

Asked By: Navis

||

Answers:

To reset the palette back to the system default, you can call:

QApplication.setPalette(QApplication.style().standardPalette())

This fetches the palette from the current style, which will be the system default.

Answered By: ToahTech
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.