用pyautogui操作windows

鼠标基本操作

0,0       X increases -->
+---------------------------+
|                           | Y increases
|                           |     |
|   1920 x 1080 screen      |     |
|                           |     V
|                           |
|                           |
+---------------------------+ 1919, 1079

这是鼠标操作的(x,y)坐标图。可以看出原点位于屏幕左上角,水平为x方向,垂直为y方向,每一个点都可以用坐标点来表示。看到这里,我仿佛回到了初三的数学课堂……

确定x,y在屏幕上,用函数onscreen

pyautogui.onScreen(0,0)
True

确定屏幕尺寸,用函数sizi()

pyautogui.size()

移动鼠标

pyautogui.moveTo(100, 200) #绝对移动
pyautogui.moveRel(100, 200)#相对移动

拖拽鼠标

pyautogui.dragTo(100, 200, button='left')#绝对移动
pyautogui.dragRel(30, 0, 2, button='right') #相对移动

渐变移动(不重要)

扫描二维码关注公众号,回复: 5535203 查看本文章
pyautogui.moveTo(100, 100, 2, pyautogui.easeInQuad)     # start slow, end fast
pyautogui.moveTo(100, 100, 2, pyautogui.easeOutQuad)    # start fast, end slow
pyautogui.moveTo(100, 100, 2, pyautogui.easeInOutQuad)  # start and end fast, slow in middle
pyautogui.moveTo(100, 100, 2, pyautogui.easeInBounce)   # bounce at the end
pyautogui.moveTo(100, 100, 2, pyautogui.easeInElastic)  # rubber band at the end

鼠标点击

pyautogui.click(x=100, y=200)
pyautogui.click(clicks=2) 
pyautogui.doubleClick(buttton='right')

鼠标按下和抬起

pyautogui.mouseDown()
pyautogui.mouseUp()

拖拽鼠标

pyautogui.scroll(10, x=100, y=100)  # move mouse cursor to 100, 200, then scroll up 10 "clicks"

键盘基本操作

输入字符

pyautogui.typewrite('Hello world!')  

按下键,抬起键和press(可以传列表)

yautogui.keyDown('shift')  # hold down the shift key
pyautogui.press('left')     # press the left arrow key
pyautogui.press('left')     # press the left arrow key
pyautogui.press('left')     # press the left arrow key
pyautogui.keyUp('shift')    # release the shift key

组合热键

pyautogui.hotkey('ctrl', 'shift', 'esc')

消息框设置

pyautogui提供了消息框功能,分别问确认方式、确认和取消方式和输入框模式

pyautogui.alert('This displays some text with an OK button.')
pyautogui.confirm('This displays text and has an OK and Cancel button.')
'OK'
pyautogui.prompt('This lets the user type in a string and press OK.')
'This is what I typed in.'

截屏功能

为了方便屏幕点击,pyautogui提供了一个截屏功能,具体如下:

  • 可以直接截屏,当变量使用
  • 也可以截屏,保存在当前文件夹
  • 截屏功能有一个region函数,用来选择性截屏,用一个tuple来表示left,top,width,height
  • 截屏后可以用locateOnScreen()来测量截屏大小
  • center()函数用了确定截屏中心点x,y
  • 确定某点的颜色
im1 = pyautogui.screenshot()
im2 = pyautogui.screenshot('my_screenshot.png')
im = pyautogui.screenshot(region=(0,0, 300, 400))
pyautogui.locateOnScreen('calc7key.png')
pyautogui.center(button7location)
pyautogui.locateCenterOnScreen('calc7key.png')
im = pyautogui.screenshot()
im.getpixel((100, 200))

实际操作

通过“locateOnScreen” 定位 按钮图片 的坐标,然后就可以直接操作了。

#屏幕截图
pic = auto.screenshot('my_screenshot.png') #在1920 x 1080屏幕上,screenshot()函数大约需要100毫秒
#图片必须在pic内可见即窗口在最前端
start_location = auto.locateOnScreen('start.png')   #传入[开始]按钮状态1的图片
if start_location!=None:
    x,y = auto.center(start_location)#转化为 x,y坐标
    print("坐标:",x,y)#按键的坐标
    auto.click(x,y)


作者:一言不合就跑步
链接:https://www.jianshu.com/p/ff337d381a64

猜你喜欢

转载自www.cnblogs.com/zpw-1/p/10532659.html