darkdetect

repository·master·Indexed 42 days ago

https://github.com/albertosottile/darkdetect

A lightweight Python package to detect OS Dark Mode on macOS 10.14+, Windows 10 1607+, and Linux (GTK). It provides functions like theme(), isDark(), and isLight() to check system appearance, as well as a listener() function to monitor theme changes via callbacks.

Tokens
1.2K
Snippets
8
Records
9
Agent score
3%

What's inside darkdetect

  1. How darkdetect works on different platforms

    master

    darkdetect provides theme detection for several environments:

    • macOS: Supports macOS 10.14+. Note that detection of the dark menu bar and dock (available since macOS 10.10) is not supported.
    • Windows: Supports Windows 10 1607+.
    • Linux: Uses an experimental detection method based on the presence of a dark GTK theme.

    If the package is executed on an operating system that does not support dark mode detection, it will return None for theme queries.

  2. Install darkdetect

    master

    You can install darkdetect via PyPI using pip.

    To enable the macOS listener functionality, you must install the optional macos-listener extra.

    Alternatively, you can vendor a copy of the package directly into your application.

    ```bash
    # Standard installation
    pip install darkdetect
    
    # Installation with macOS listener support
    pip install darkdetect[macos-listener]
    ```埋
  3. Listen for dark mode changes

    master

    You can monitor changes to the system theme by using darkdetect.listener. This function starts a listener that calls a provided callback whenever the OS switches between Dark and Light modes.

    Because darkdetect.listener is a blocking call, it is typically run in a background daemon thread to avoid freezing your main application.

    The callback function receives a single string argument: either "Dark" or "Light".

    Note: Enabling the macOS listener requires the darkdetect[macos-listener] installation.

    import threading
    import darkdetect
    
    # Define your callback function
    def my_callback(theme_name: str):
        print(f"Theme changed to: {theme_name}")
    
    # Start the listener in a daemon thread
    t = threading.Thread(target=darkdetect.listener, args=(my_callback,))
    t.daemon = True
    t.start()
  4. Detect the current system theme

    master

    Use darkdetect.theme() to get the current theme name as a string, or use the boolean helpers isDark() and isLight() to check the state.

    Supported platforms:

    • macOS 10.14+
    • Windows 10 1607+
    • Linux with a dark GTK theme

    Note: On unsupported operating systems, the package will return None instead of a theme name.

    import darkdetect
    
    # Get theme name ('Dark' or 'Light')
    print(darkdetect.theme())
    
    # Boolean checks
    print(darkdetect.isDark())
    print(darkdetect.isLight())
  5. Listen for system theme changes with listener()

    master

    The listener(callback) function allows you to register a callback that is triggered whenever the system theme changes. The callback must accept a single string argument representing the new theme ('dark' or 'light').

    import darkdetect
    
    def my_callback(theme: str):
        print(f"The theme changed to: {theme}")
    
    darkdetect.listener(my_callback)
    
    # The script must remain running to receive events
    import time
    time.sleep(100)