5yutan5 / PyQtDarkTheme

A flat dark theme for PySide and PyQt.
https://pyqtdarktheme.readthedocs.io
MIT License
545 stars 84 forks source link

How to get the theme name currently set by the application? #222

Closed crazy-zxx closed 1 year ago

crazy-zxx commented 1 year ago

When setting the theme to 'auto' to switch automatically, how do I know in the application whether the current theme used by the application is light or dark? I don't see any property or method that can be called directly.

Thanks.

fieschkon commented 1 year ago

Give this a try. Perhaps this function can be exposed for a more elegant solution. This should correspond to what the 'auto' theme selects.

from qdarktheme import _style_loader

theme = _style_loader._detect_system_theme('light')

Where 'light' is the default if PyQtDarkTheme cannot determine what the native OS theme is.

crazy-zxx commented 1 year ago

thanks

5yutan5 commented 1 year ago

Hi @crazy-zxx. and thank you @fieschkon. There is no public function to get the current theme. The method shown by fieschkon is used private function and may change in the future. Maybe I will change this private function to public fuction in the future.

jamesdbrock commented 1 year ago

I would also like this feature. I wrote this. It’s gross but it seems to work. There seems to be no way of figuring out whether the current active theme is "light" or "dark" without remembering ourselves what we set it to.

_theme_set = []

def getTheme() -> str:
    """
    Return the current active theme, either "light" or "dark".
    """
    if len(_theme_set) > 0:
        set_theme = _theme_set[0]
    else:
        set_theme = "light"

    if set_theme == "auto":
        system_theme = darkdetect.theme().lower()
        if system_theme is None:
            return set_theme
        return system_theme
    return set_theme

def setTheme(theme:str):
    """
    Set the active theme to "light", "dark" or "auto".
    """
    if len(_theme_set) == 0:
        _theme_set.append(theme)
    else:
        _theme_set[0] = theme

    qdarktheme.setup_theme(theme)
    qpalette = qdarktheme.load_palette(theme)

    if getTheme() == "dark":
        # In Windows set titlebar black
        # https://github.com/5yutan5/PyQtDarkTheme/issues/229
        qpalette.setColor(QPalette.WindowText, QColorConstants.White)
        QApplication.instance().setPalette(qpalette)