Detect dark mode in macos from python

。_饼干妹妹 提交于 2020-01-05 02:52:26

问题


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.


回答1:


Building on this question, you could install pyobjc and use NSUserDefaults:

>>> from Foundation import NSUserDefaults
>>> NSUserDefaults.standardUserDefaults().stringForKey_('AppleInterfaceStyle')
'Dark'



回答2:


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.



来源:https://stackoverflow.com/questions/54701288/detect-dark-mode-in-macos-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!