寫在前面的話
最近看了一篇大神用pyautogui和pywin32寫出跳一跳外掛的文章旦事,心里很是佩服忧便。于是今天一摸電腦就先看了pywin32族吻,你猜怎么著?
完全看不懂珠增!你沒有看錯超歌,好復(fù)雜啊。
老師教導(dǎo)我們碰到看不懂的題目怎么辦切平?當(dāng)然是放棄啦握础,哈哈哈哈!(不要打我)
轉(zhuǎn)而看pyautogui悴品,這個還行禀综,不是很復(fù)雜,一天下來 苔严,基本已經(jīng)掌握定枷。本著好東西要分享的原則,根據(jù)官方文檔的順序?qū)⑹褂梅椒ń榻B如下:
鼠標基本操作
0,0 X increases -->
+---------------------------+
| | Y increases
| | |
| 1920 x 1080 screen | |
| | V
| |
| |
+---------------------------+ 1919, 1079
這是鼠標操作的(x,y)坐標圖届氢∏分希可以看出原點位于屏幕左上角,水平為x方向,垂直為y方向岖妄,每一個點都可以用坐標點來表示型将。看到這里荐虐,我仿佛回到了初三的數(shù)學(xué)課堂……
確定x,y在屏幕上七兜,用函數(shù)onscreen
pyautogui.onScreen(0,0)
True
確定屏幕尺寸,用函數(shù)sizi()
pyautogui.size()
移動鼠標
pyautogui.moveTo(100, 200) #絕對移動
pyautogui.moveRel(100, 200)#相對移動
拖拽鼠標
pyautogui.dragTo(100, 200, button='left')#絕對移動
pyautogui.dragRel(30, 0, 2, button='right') #相對移動
漸變移動(不重要)
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')
消息框設(shè)置
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提供了一個截屏功能狠裹,具體如下:
- 可以直接截屏,當(dāng)變量使用
- 也可以截屏汽烦,保存在當(dāng)前文件夾
- 截屏功能有一個region函數(shù)涛菠,用來選擇性截屏,用一個tuple來表示left,top,width,height
- 截屏后可以用locateOnScreen()來測量截屏大小
- center()函數(shù)用了確定截屏中心點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))