为了获取页面窗口的光标位置和相应的RGB值,使用如下代码:
import pyautogui
import time
# 获取屏幕光标位置
def get_mouse_positon():
time.sleep(1) # 延迟1s后开始
print('开始获取鼠标位置')
try:
# for i in range(16): # 对屏幕光标获取16次坐标点和RGB值
while True:
# Get and print the mouse coordinates.
x, y = pyautogui.position()
positionStr = '鼠标坐标点(X,Y):{},{}'.format(str(x).rjust(4), str(y).rjust(4))
pix = pyautogui.screenshot().getpixel((x, y)) # 获取屏幕光标所在屏幕点的RGB颜色
positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'
print(positionStr)
time.sleep(0.5) # 间隔时间
except:
print('获取鼠标位置失败')
if __name__ == "__main__":
get_mouse_positon()