用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()ui

pyautogui.size()

 

移动鼠标spa

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

 

拖拽鼠标code

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

 

渐变移动(不重要)blog

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')

 

鼠标按下和抬起get

pyautogui.mouseDown()
pyautogui.mouseUp()

 

拖拽鼠标数学

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

 

键盘基本操做

输入字符string

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” 定位 按钮图片 的坐标,而后就能够直接操做了。

locateCenterOnScreen(image, grayscale=True),参数grayscale是是否转灰度,十分有用

#屏幕截图
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

相关文章
相关标签/搜索