Skip to main content

Detect OS Dark Mode from Python

Project description

Darkdetect-angr

This package allows to detect if the user is using Dark Mode on:

The main application of this package is to detect the Dark mode from your GUI Python application (Tkinter/wx/pyqt/qt for python (pyside)/...) and apply the needed adjustments to your interface. Darkdetect is particularly useful if your GUI library does not provide a public API for this detection (I am looking at you, Qt). In addition, this package does not depend on other modules or packages that are not already included in standard Python distributions.

Install

The preferred channel is PyPI:

pip install darkdetect_angr

Alternatively, you are free to vendor directly a copy of Darkdetect in your app. Further information on vendoring can be found here.

macOS Listener Support

To enable the macOS listener, additional components are required, these can be installed via:

pip install darkdetect[macos-listener]

Usage

import darkdetect

>>> darkdetect.theme()
'Dark'

>>> darkdetect.isDark()
True

>>> darkdetect.isLight()
False

It's that easy.

Listener

darkdetect_angr exposes a listener API which is far more efficient than busy waiting on theme() changes. This API is exposed primarily via a Listener class. Detailed API documentation can be found here. For a quick overview: the darkdetect.Listener class exposes the following methods / members:

  1. .__init__(callback: Optional[Callable[[str], None]]): The constructor simply sets .callback to the given callback argument
  2. .callback: Optional[Callable[[str], None]]: The settable callback function that the listener uses; it will be passed "Dark" or "Light" when the theme is changed.
  3. .listen(): This starts listening for theme changes, it will invoke self.callback(theme_name) when a change is detected.
  4. .stop(timeout: Optional[int]) -> bool: This function attempts to stop the listener, waiting at most timeout seconds (None means infinite), returning True on success, False on timeout. Regardless of the result, after .stop returns, theme changes will no longer trigger callback, though running callbacks will not be interrupted. .stop may safely be re-invoked any number of times, but must succeed at before re-calling .listen().
Wrapper Function

The simplest method of using this API is the darkdetect.listener function, which takes a callback function as an argument. This function is a small wrapper around Listener(callback).listen(). In this mode, the listener cannot be stopped; forceful stops may not clean up resources (such as subprocesses if applicable).

Examples

Below are 2 examples of basic usage; additional examples can be found here.

A simple listener:
import threading
import darkdetect

listener = darkdetect.Listener(print)
t = threading.Thread(target=listener.listen, daemon=True)
# OR: t = threading.Thread(target=darkdetect.listener, args=(print,), daemon=True)
t.start()
User input controlling listener
import threading
import darkdetect
import time

listener = darkdetect.Listener(print)
t = threading.Thread(target=listener.listen)
t.start()

txt = ""
while txt != "quit":
  txt = input()
  if txt == "print":
    listener.callback = print
  elif txt == "verbose":
    listener.callback = lambda x: print(f"The theme changed to {x} as {time.time()}")
listener.stop(0)

print("Waiting for running callbacks to complete and the listener to terminate")
if not listener.stop(timeout=5):
  print("Callbacks/listener are still running after 5 seconds!")

Known Issues

  1. On macOS, detection of the dark menu bar and dock option (available from macOS 10.10) is not supported.
  2. On macOS, using the listener API in a bundled app where sys.executable is not a python interpreter is not supported (though it may still work as it uses the same code path as multiprocessing).
  3. On Windows, the after Listener.stop(None) is not supported as it may not die until another theme change is detected. Future invocations of callback will not be made, but the listener itself will persist.

Notes

  • This software is licensed under the terms of the 3-clause BSD License.
  • This package can be installed on any operative system, but theme(), isDark(), and isLight() will always return None unless executed on a OS that supports Dark Mode, including older versions of macOS and Windows.
  • Details on the detection method used on macOS.
  • Details on the experimental detection method used on Linux.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

darkdetect_angr-0.9.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

darkdetect_angr-0.9.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file darkdetect_angr-0.9.1.tar.gz.

File metadata

  • Download URL: darkdetect_angr-0.9.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for darkdetect_angr-0.9.1.tar.gz
Algorithm Hash digest
SHA256 78a608e193b023a679a901d3f77bc018b1323addcebb30584f3777892b1fd20c
MD5 8a7a8e79a59739d2cc44c986a0438b0a
BLAKE2b-256 19b6e1c27de08839daafb7719ea48edd2b264ea3ccf77f8197e9ac02cfc29646

See more details on using hashes here.

File details

Details for the file darkdetect_angr-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for darkdetect_angr-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eeb02537e5ecadd50788406b9d66747be15a8a5cb8418e864314291136f1b16e
MD5 c220f744f7b0f6947ed020a5c4802857
BLAKE2b-256 3f473d9c54c0bd36fdb3fe4d930be5ac5268518233ecb266613e204afb0c43e8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page