Github: https://github.com/asweigart/pyautogui
PyAutoGUI
PyAutoGUIは、人間のためのクロスプラットフォームのGUIオートメーションPythonモジュールです。 マウスとキーボードをプログラムで制御するために使用します。
https://pyautogui.readthedocs.orgで入手できる完全なドキュメント
簡体字中国語のドキュメント(简体中文版文档)は、 https: //muxuezi.github.io/posts/doc-pyautogui.htmlから入手できます。
ソースコードはhttps://github.com/asweigart/pyautoguiで入手できます。
依存関係
pipを使用してPyPIからPyAutoGUIをインストールする場合:
Windowsは依存関係がありません。 Win32拡張機能はインストールする必要はありません。
OS Xには、pyobjc-coreとpyobjcモジュールが(この順序で)インストールされている必要があります。
Linuxにはpython3-xlib(またはPython-2のPython-xlib)モジュールがインストールされている必要があります。
Pillowをインストールする必要があります.Linuxでは、PillowのPNG / JPEGが正しく動作するようにライブラリを追加する必要があります。 見る:
https://stackoverflow.com/questions/7648200/pip-install-pil-e-tickets-1-no-jpeg-png-support
http://ubuntuforums.org/showthread.php?t=1751455
開発を行いPyAutoGUIに貢献したい場合は、PyPIからこれらのモジュールをインストールする必要があります:
- 淡水
- pymsgbox
- ファイティング
使用例
キーボードとマウスのコントロール
>>> import pyautogui
>>> screenWidth, screenHeight = pyautogui.size()
>>> currentMouseX, currentMouseY = pyautogui.position()
>>> pyautogui.moveTo(100, 150)
>>> pyautogui.click()
>>> pyautogui.moveRel(None, 10) # move mouse 10 pixels down
>>> pyautogui.doubleClick()
>>> pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds.
>>> pyautogui.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key
>>> pyautogui.press('esc')
>>> pyautogui.keyDown('shift')
>>> pyautogui.typewrite(['left', 'left', 'left', 'left', 'left', 'left'])
>>> pyautogui.keyUp('shift')
>>> pyautogui.hotkey('ctrl', 'c')
メッセージボックスを表示する
>>> import pyautogui
>>> pyautogui.alert('This is an alert box.')
'OK'
>>> pyautogui.confirm('Shall I proceed?')
'Cancel'
>>> pyautogui.confirm('Enter option.', buttons=['A', 'B', 'C'])
'B'
>>> pyautogui.prompt('What is your name?')
'Al'
>>> pyautogui.password('Enter password (text will be hidden)')
'swordfish'
スクリーンショット機能
(PyAutoGUIは画像関連の機能として枕を使用しています)。
>>> import pyautogui
>>> im1 = pyautogui.screenshot()
>>> im1.save('my_screenshot.png')
>>> im2 = pyautogui.screenshot('my_screenshot2.png')
画像が画面上のどこにあるかを見つけることもできます:
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('button.png') # returns (left, top, width, height) of matching region
>>> button7location
(1416, 562, 50, 41)
>>> buttonx, buttony = pyautogui.center(button7location)
>>> buttonx, buttony
(1441, 582)
>>> pyautogui.click(buttonx, buttony) # clicks the center of where the button was found
locateCenterOnScreen()関数は、このマッチ領域の中心を返します:
>>> import pyautogui
>>> buttonx, buttony = pyautogui.locateCenterOnScreen('button.png') # returns (x, y) of matching region
>>> buttonx, buttony
(1441, 582)
>>> pyautogui.click(buttonx, buttony) # clicks the center of where the button was found