Package: darkdetect-angr
Darkdetect-angrThis package allows to detect if the user is using Dark Mode on:macOS 10.14+Windows 10 1607+Linux witha dark GTK theme.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 librarydoes notprovide 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.InstallThe preferred channel is PyPI:pipinstalldarkdetect_angrAlternatively, you are free to vendor directly a copy of Darkdetect in your app. Further information on vendoring can be foundhere.macOS Listener SupportTo enable the macOS listener, additional components are required, these can be installed via:pipinstalldarkdetect[macos-listener]Usageimportdarkdetect>>>darkdetect.theme()'Dark'>>>darkdetect.isDark()True>>>darkdetect.isLight()FalseIt's that easy.Listenerdarkdetect_angrexposes a listener API which is far more efficient than busy waiting ontheme()changes.
This API is exposed primarily via aListenerclass.
Detailed API documentation can be foundhere.
For a quick overview: thedarkdetect.Listenerclass exposes the following methods / members:.__init__(callback: Optional[Callable[[str], None]]): The constructor simply sets.callbackto the given callback argument.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..listen(): This starts listening for theme changes, it will invokeself.callback(theme_name)when a change is detected..stop(timeout: Optional[int]) -> bool:
This function attempts to stop the listener,
waiting at mosttimeoutseconds (Nonemeans infinite),
returningTrueon success,Falseon timeout.
Regardless of the result, after.stopreturns, theme changes
will no longer triggercallback, though running callbacks will
not be interrupted..stopmay safely be re-invoked any number of times, but must succeed at before re-calling.listen().Wrapper FunctionThe simplest method of using this API is thedarkdetect.listenerfunction,
which takes a callback function as an argument.
This function is a small wrapper aroundListener(callback).listen().In this mode, the listener cannot be stopped; forceful stops may not clean up resources (such as subprocesses if applicable).ExamplesBelow are 2 examples of basic usage; additional examples can be foundhere.A simple listener:importthreadingimportdarkdetectlistener=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 listenerimportthreadingimportdarkdetectimporttimelistener=darkdetect.Listener(print)t=threading.Thread(target=listener.listen)t.start()txt=""whiletxt!="quit":txt=input()iftxt=="print":listener.callback=printeliftxt=="verbose":listener.callback=lambdax: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")ifnotlistener.stop(timeout=5):print("Callbacks/listener are still running after 5 seconds!")Known IssuesOn macOS, detection of the dark menu bar and dock option (available from macOS 10.10) is not supported.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 asmultiprocessing).On Windows, the afterListener.stop(None)is not supported as it may not die until another theme change is detected.
Future invocations ofcallbackwill not be made, but the listener itself will persist.NotesThis software is licensed under the terms of the 3-clause BSD License.This package can be installed on any operative system, buttheme(),isDark(), andisLight()will always returnNoneunless executed on a OS that supports Dark Mode, including older versions of macOS and Windows.Detailson the detection method used on macOS.Detailson the experimental detection method used on Linux.