### Install Darkdetect via Pip Source: https://github.com/albertosottile/darkdetect/blob/master/README.md Install the package using pip. This is the recommended installation method. ```bash pip install darkdetect ``` -------------------------------- ### Install macOS Listener Components Source: https://github.com/albertosottile/darkdetect/blob/master/README.md To enable the listener functionality on macOS, install the optional components using pip. ```bash pip install darkdetect[macos-listener] ``` -------------------------------- ### Install Darkdetect Source: https://context7.com/albertosottile/darkdetect/llms.txt Commands for installing the library via pip, including an optional feature for macOS listener support. ```bash # Basic installation pip install darkdetect # With macOS listener support (requires pyobjc) pip install darkdetect[macos-listener] ``` -------------------------------- ### Monitor Theme Changes with listener() Source: https://context7.com/albertosottile/darkdetect/llms.txt Starts a blocking listener that executes a callback function upon system theme changes. Should be run in a separate thread to avoid blocking the main application. ```python import darkdetect import threading # Simple callback function def on_theme_change(new_theme): print(f"Theme changed to: {new_theme}") # Update your application's appearance here # Run listener in a daemon thread listener_thread = threading.Thread(target=darkdetect.listener, args=(on_theme_change,)) listener_thread.daemon = True listener_thread.start() ``` -------------------------------- ### Get Current OS Theme Source: https://github.com/albertosottile/darkdetect/blob/master/README.md Use `darkdetect.theme()` to get the current OS theme ('Dark' or 'Light'). This function returns 'None' if the OS does not support dark mode. ```python import darkdetect >>> darkdetect.theme() 'Dark' ``` ```python >>> darkdetect.isDark() True ``` ```python >>> darkdetect.isLight() False ``` -------------------------------- ### Check theme via CLI and Python Source: https://context7.com/albertosottile/darkdetect/llms.txt Demonstrates how to check the current system theme using the command line module or the equivalent Python function call. ```bash # Check current theme from command line python -m darkdetect # Output: Current theme: Dark ``` ```python # Equivalent to running the module import darkdetect print('Current theme: {}'.format(darkdetect.theme())) ``` -------------------------------- ### theme() Source: https://context7.com/albertosottile/darkdetect/llms.txt Retrieves the current operating system theme as a string. ```APIDOC ## theme() ### Description Returns the current OS theme as a string: 'Dark', 'Light', or None if detection is not supported on the current platform. ### Response - **Return Value** (string|None) - The current theme name or None. ``` -------------------------------- ### listener(callback) Source: https://context7.com/albertosottile/darkdetect/llms.txt Monitors for OS theme changes and triggers a callback. ```APIDOC ## listener(callback) ### Description Creates a blocking listener that monitors for OS theme changes and calls the provided callback function with 'Dark' or 'Light' when the theme changes. This function should be run in a separate thread. ### Parameters - **callback** (function) - Required - A function that accepts a single string argument representing the new theme. ``` -------------------------------- ### Listen for OS Theme Changes Source: https://github.com/albertosottile/darkdetect/blob/master/README.md Create a listener thread to be notified when the OS theme changes. Pass a callback function that accepts a string ('Dark' or 'Light') as an argument. This requires the `threading` module. ```python import threading import darkdetect t = threading.Thread(target=darkdetect.listener, args=(print,)) t.daemon = True t.start() ``` -------------------------------- ### Detect Theme with theme() Source: https://context7.com/albertosottile/darkdetect/llms.txt Retrieves the current OS theme as a string or None if unsupported. Useful for mapping theme states to application color configurations. ```python import darkdetect # Get the current theme current_theme = darkdetect.theme() print(f"Current theme: {current_theme}") # Output: Current theme: Dark # Use theme detection to configure application styling def get_app_colors(): theme = darkdetect.theme() if theme == 'Dark': return { 'background': '#1e1e1e', 'foreground': '#ffffff', 'accent': '#0078d4' } elif theme == 'Light': return { 'background': '#ffffff', 'foreground': '#000000', 'accent': '#0067c0' } else: # Fallback for unsupported platforms return { 'background': '#ffffff', 'foreground': '#000000', 'accent': '#0067c0' } colors = get_app_colors() print(f"Using colors: {colors}") ``` -------------------------------- ### Implement cross-platform theme detection Source: https://context7.com/albertosottile/darkdetect/llms.txt A robust pattern for handling theme detection with a fallback mechanism for unsupported platforms. ```python import darkdetect import sys # Handle cross-platform detection gracefully def get_theme_safe(): """Get theme with fallback for unsupported platforms.""" theme = darkdetect.theme() if theme is None: print(f"Warning: Dark mode detection not supported on {sys.platform}") return 'Light' # Default fallback return theme # Platform-specific behavior documentation: # - macOS: Reads AppleInterfaceStyle from NSUserDefaults # - Windows: Reads AppsUseLightTheme from Windows Registry # - Linux: Uses gsettings to check GNOME color-scheme or gtk-theme current = get_theme_safe() print(f"Using theme: {current}") ``` -------------------------------- ### isLight() Source: https://context7.com/albertosottile/darkdetect/llms.txt Checks if the system is currently in Light Mode. ```APIDOC ## isLight() ### Description Returns True if Light Mode is enabled, False if Dark Mode is enabled, or None if detection is not supported. ### Response - **Return Value** (boolean|None) - True if Light Mode is active, False if Dark Mode is active, or None. ``` -------------------------------- ### Manage application theme state Source: https://context7.com/albertosottile/darkdetect/llms.txt A class-based approach to track the current theme and register callbacks for real-time updates using a background listener thread. ```python class ThemeManager: def __init__(self): self.current_theme = darkdetect.theme() or 'Light' self.callbacks = [] self._start_listener() def _start_listener(self): def _on_change(theme): self.current_theme = theme for callback in self.callbacks: callback(theme) thread = threading.Thread(target=darkdetect.listener, args=(_on_change,)) thread.daemon = True thread.start() def register_callback(self, callback): self.callbacks.append(callback) # Immediately call with current theme callback(self.current_theme) # Usage manager = ThemeManager() manager.register_callback(lambda t: print(f"App notified: {t}")) # Keep main thread alive for testing # import time # while True: # time.sleep(1) ``` -------------------------------- ### isDark() Source: https://context7.com/albertosottile/darkdetect/llms.txt Checks if the system is currently in Dark Mode. ```APIDOC ## isDark() ### Description Returns True if Dark Mode is enabled, False if Light Mode is enabled, or None if detection is not supported. ### Response - **Return Value** (boolean|None) - True if Dark Mode is active, False if Light Mode is active, or None. ``` -------------------------------- ### Check Light Mode with isLight() Source: https://context7.com/albertosottile/darkdetect/llms.txt Returns a boolean indicating if light mode is active. Useful for selecting assets or palettes based on light mode preference. ```python import darkdetect # Check if light mode is active if darkdetect.isLight(): print("Light mode is enabled") icon_set = "icons/light/" elif darkdetect.isLight() is False: print("Dark mode is enabled") icon_set = "icons/dark/" else: print("Theme detection not supported") icon_set = "icons/default/" # Example: PyQt5 application theme selection """ from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QPalette, QColor def apply_theme(app): if darkdetect.isLight(): # Use default light palette app.setStyle('Fusion') palette = QPalette() palette.setColor(QPalette.Window, QColor(240, 240, 240)) palette.setColor(QPalette.WindowText, QColor(0, 0, 0)) app.setPalette(palette) else: # Apply dark palette app.setStyle('Fusion') palette = QPalette() palette.setColor(QPalette.Window, QColor(53, 53, 53)) palette.setColor(QPalette.WindowText, QColor(255, 255, 255)) app.setPalette(palette) """ ``` -------------------------------- ### Check Dark Mode with isDark() Source: https://context7.com/albertosottile/darkdetect/llms.txt Returns a boolean indicating if dark mode is active. Commonly used for conditional UI styling in frameworks like Tkinter. ```python import darkdetect # Check if dark mode is active if darkdetect.isDark(): print("Dark mode is enabled") stylesheet = "dark_theme.css" elif darkdetect.isDark() is False: print("Light mode is enabled") stylesheet = "light_theme.css" else: print("Theme detection not supported on this platform") stylesheet = "default_theme.css" # Practical example: Tkinter application with theme detection import tkinter as tk def create_themed_window(): root = tk.Tk() root.title("Themed App") if darkdetect.isDark(): root.configure(bg='#2d2d2d') label = tk.Label(root, text="Dark Mode Active", bg='#2d2d2d', fg='white') else: root.configure(bg='#f0f0f0') label = tk.Label(root, text="Light Mode Active", bg='#f0f0f0', fg='black') label.pack(padx=20, pady=20) return root # window = create_themed_window() # window.mainloop() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.