模拟鼠标键盘,查看鼠标在屏幕的位置

 1 # 案例获取鼠标的位置,方便复制我们定位的鼠标坐标点到代码中
 2 import pyautogui
 3 import time
 4 
 5 # 获取鼠标位置
 6 def get_mouse_positon():
 7   time.sleep(5) # 准备时间
 8   print('开始获取鼠标位置')
 9   try:
10     for i in range(10):
11       # Get and print the mouse coordinates.
12       x, y = pyautogui.position()
13       positionStr = '鼠标坐标点(X,Y)为:{},{}'.format(str(x).rjust(4), str(y).rjust(4))
14       pix = pyautogui.screenshot().getpixel((x, y)) # 获取鼠标所在屏幕点的RGB颜色
15       positionStr += ' RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(
16         3) + ')'
17       print(positionStr)
18       time.sleep(0.5) # 停顿时间
19   except:
20     print('获取鼠标位置失败')
21 
22 
23 if __name__ == "__main__":
24   get_mouse_positon()

猜你喜欢

转载自www.cnblogs.com/chunfang/p/13371078.html