Task list: 😄
GitHub (number of stars)(2018, April, 01)
StackOverflow (number of tagged questions)
PyAutoGUI vs PyWinAuto Python PyAutoGUI和Pywinauto区别及安装
” 首先,这两者最大共同点就是可以操作计算机,模拟人工输入和鼠标操作等等。不过这两者也有侧重点,PyAutoGUI侧重于鼠标,键盘,截图,消息框的功能,Pywinauto侧重对CS的操作,虽然都有键盘,鼠标等模拟输入,不过最核心还是软件上的操作比较多。可以说各有所长。”
kill -9
?暫停與失效安全保護
pyautogui.PAUSE = 1.5
暫停 1.5 秒pyautogui.FAILSAFE = False
官方文件的大綱:
0,0 X increases -->
+---------------------------+
| | Y increases
| | |
| 1920 x 1080 screen | |
| | V
| |
| |
+---------------------------+ 1919, 1079
>>> import pyautogui
>>> pyautogui.size()
(1920, 1080)
>>> width, height = pyautogui.size()
取得滑鼠位置
>>> pyautogui.postion()
(311,32) # 傳回目前游標座標
移動滑鼠游標
pyautogui.moveTo(xpos,ypos,duration=xxsec)
:
duration
是每次移動幾秒,直到到達目的座標
pyautogui.moveRel(xdis,ydis,duration=xxsec)
import pyautogui
print('Press Ctrl-C to quit')
try:
while True:
x,y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
# str.rjust() 字串向右對齊
print(positionStr,end='')
print('\b'*len(positionStr), end='', flush=True)
# 字串印出後,用等長的 Backspace 刪除,並 flush
except KeyboardInterrupt:
print('\nDone.')
Click
pyautogui.click(xpos,ypos,button='left') # right,middle,left
pyautogui.click(xpos,ypos)
pyautogui.mouseUp()
pyautogui.mouseDown()
pyautogui.doubleClick()
pyautogui.rightClick()
pyautogui.middleClick()
Drag and Drop
# spiralDraw.py
import pyautogui, time
time.sleep(5) # 給緩衝時間
pyautogui.click() # get focus
distance = 200
while distance > 0:
pyautogui.dragRel(distance, 0, duration = 0.2) # move right
distance = distance -5
pyautogui.dragRel(0,distance, duration = 0.2) # move down
pyautogui.dragRel(-distance,0, duration = 0.2) # move left
distance = distance -5
pyautogui.dragRel(0, -distance, duration = 0.2) # move up
pyautogui.scroll(xdistance)
:>>> pyautogui.scroll(10) # scroll up 10 "clicks"
>>> pyautogui.scroll(-10) # scroll down 10 "clicks"
>>> pyautogui.scroll(10, x=100, y=100) # 游標先移到 x, y 後,再移動 10 "步"
# pyperclip.py
import pyperclip # 傳資料到剪貼簿的工具
numbers = ''
for i in range(200):
numbers = numbers + str(i) + '\n'
pyperclip.copy(numbers)
# 開個 notepad ,把東西貼上去
import time, pyautogui
time.sleep(5); pyautogui.scroll(100)
截圖
>>> import pyautogui
>>> im = pyautogui.screenshot()
# im 就是 Pillow 的 Image 物件
>>> im.getpixel((0,0))
# 傳回值是座標的 (R, G, B)
(176,176,175)
>>> im.getpixel((50,200))
(130,135,144)
分析截圖
pyautogui.pixelMatchsColor(xpos,ypos,(R,G,B))
pass
pyautogui.locateOnScreen(
path2pic)
找出小圖的位置在目前螢幕的某個位置pyautogui.typewrite('string')
SEE ALSO
pyautogui.keyUp('shift')
,pyautogui.keyDown('ctrl')
pyautogui.press('a')
= keyDown + keyUppyautogui.hotkey('ctrl','alt','shift','s')
可以同時按下多鍵pyautogui.FAILSAFE = True
, 打開安全防護功能。
滑鼠在移出畫面邊界時,會自動停止,會觸發 pyautogui.FailSageExcetion
pyautogui.size()
傳回 寬、高pyautogui.position()
,x,y 座標pyautogui.dragTo()
, pyautogui.dragRel()
pyautogui.typewriter("Hellow World!")
pyautogui.press(
left)
im = pyaytogui.screenshot(
screenshot.png)
time.sleep(2)
pyautogui.PAUSE= 2
防閒罝動作:
import pyautogui
import time
time.sleep(5)
print('Press Ctrl-C to quit')
distance = 4
timespan = 1
try:
while True:
pyautogui.moveRel(distance,0, duration = timespan)
time.sleep(5)
pyautogui.moveRel(-distance,0, duration = timespan)
except KeyboardInterrupt:
print('\nDone.')
pass
pass