Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect dark mode in macos from python

I am writing a PyQt app and I have to add a patch so that the font is readable on macos with dark mode enabled:

app = QApplication([])
# Fix for the font colours on macos when running dark mode
if sys.platform == 'darwin':
    p = app.palette()
    p.setColor(QPalette.Base, QColor(101, 101, 101))
    p.setColor(QPalette.ButtonText, QColor(231, 231, 231))
    app.setPalette(p)
main_window = MainWindow()
main_window.show()
app.exec_()

The issue with this patch is then it makes things unreadable on macos with light mode.

Is there a way I can detect dark mode on macos from python or using a standard shell command through subprocess?

EDIT: As of PyQt 5.12 the dark mode fix is no longer required.

like image 993
Crunchy234 Avatar asked Oct 17 '25 10:10

Crunchy234


1 Answers

In case you do not want to import pyobjc, you can use Darkdetect, a dedicated package that uses only dependencies provided with standard Python distributions.

Usage:

import darkdetect

>>> darkdetect.theme()
'Dark'

>>> darkdetect.isDark()
True

>>> darkdetect.isLight()
False

Darkdetect is also available on PyPI: pip install darkdetect.

Disclaimer: I am the author of Darkdetect.

like image 172
asottile Avatar answered Oct 20 '25 08:10

asottile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!