PyQt windows which detects Windows dark/light theme settings and changes the theme dynamically
This is for Windows only. I tested this in Windows 11. This app might work in Windows 10 as well.
This is using Windows DWMAPI
library, winreg
for get the window registry's value, nativeEvent
to change the theme.
As you might know, Windows can change the theme light to dark, or dark to light. This window detects such a thing and apply it to the frame.
You can make it unable to detect the theme if you don't want to change the current window theme accordance with the system theme. See the Method Overview below.
- PyQt5
git clone ~
or
python -m pip install pyqt-windows-os-light-dark-theme-window --upgrade
setDarkTheme(f: bool)
- If you want to set Windows theme directly, use this. If you giveTrue
, dark theme will be set to qt window.isDetectingThemeAllowed() -> bool
- Check if detecting theme feature is allowed.allowDetectingTheme(f: bool)
- Allow detecting theme to change the Windows system theme in real-time. True in default.
changedToDark(bool)
- If theme changed, this signal will be emitted.
Note: You don't need this code sample if you cloned this repo, just run the sample.py and you can see the fine result. This code sample is for the people who installed this with pip.
from PyQt5.QtWidgets import QPushButton, QGridLayout, QApplication
from pyqt_windows_os_light_dark_theme_window.main import Window
class MainWindow(Window):
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
btn = QPushButton('Theme Toggle')
btn.setCheckable(True)
btn.toggled.connect(self.__themeToggled)
lay = QGridLayout()
lay.addWidget(btn)
self.setLayout(lay)
self.changedToDark.connect(self.__darkThemeOn)
def __themeToggled(self, f):
self.setDarkTheme(f)
def __darkThemeOn(self, f):
print(f'Is current theme dark?: {f}')
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
Windows light theme (as you expected)
Windows dark theme