Search code examples
pythonpython-3.xpython-os

how to use python to get display from environment


I want to detect cursor position and colour quickly (less than 0.1 sec)

I read this article and want to try he's last method (Xlib)

However, when I run os.getenv("DISPLAY"), it returns None

Does anyone know how to fix this or offer me a new method to achieve my goal?

System: MacBook Pro, macOS 10.15.4


Solution

  • Xlib is usually not available on macOS. You may consider using a library such as pynput or pyautogui to achieve what you want to do instead.

    e.g. with pynput

    from pynput.mouse import Controller
    
    mouse = Controller()
    print(mouse.position)  # (x, y) coordinates tuple