my name
my name

Reputation: 51

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

Upvotes: 0

Views: 414

Answers (2)

Niraj Panchasara
Niraj Panchasara

Reputation: 81

Pyautogui is good library for GUI operations its having position() and i used screenshot() and then get color of given pixel, this is something i have tried, you can check it out by this below code,

Before go with code, install package by using

python -m pip install pyautogui

Code:

import pyautogui

try:
    while True:
        x, y = pyautogui.position()
        pixelColor = pyautogui.screenshot().getpixel((x, y))
        screenShot = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        screenShot += ' RGB: (' + str(pixelColor[0]).rjust(3)
        screenShot += ', ' + str(pixelColor[1]).rjust(3)
        screenShot += ', ' + str(pixelColor[2]).rjust(3) + ')'
        print(screenShot)

except KeyboardInterrupt:
    print("\nDone...")

Upvotes: 1

michaeldel
michaeldel

Reputation: 2385

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 

Upvotes: 1

Related Questions