安装代码:
pip install pyautogui
1.简介
1.1 目的
PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。可以用pip安装,Github上有源代码。
下面的代码让鼠标移到屏幕中央。
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
PyAutoGUI可以模拟鼠标的移动、点击、拖拽,键盘按键输入、按住操作,以及鼠标+键盘的热键同时按住等操作,可以说手能动的都可以。
pyautogui基础操作样例
import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 100)
pyautogui.click()
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)
pyautogui.typewrite(‘Hello world!’, interval=0.25)
pyautogui.press(‘esc’)
pyautogui.keyDown(‘shift’)
pyautogui.press([‘left’, ‘left’, ‘left’, ‘left’, ‘left’, ‘left’])
pyautogui.keyUp(‘shift’)
pyautogui.hotkey(‘ctrl’, ‘c’)
PyAutoGUI键盘表:
‘enter’ (或‘return’ 或 ‘\n’)
回车
‘esc’
ESC键
‘shiftleft’, ‘shiftright’
左右SHIFT键
‘altleft’, ‘altright’
左右ALT键
‘ctrlleft’, ‘ctrlright’
左右CTRL键
‘tab’ (‘\t’)
TAB键
‘backspace’, ‘delete’
BACKSPACE 、DELETE键
‘pageup’, ‘pagedown’
PAGE UP 和 PAGE DOWN键
‘home’, ‘end’
HOME 和 END键
‘up’, ‘down’, ‘left’, ‘right’
箭头键
‘f1’, ‘f2’, ‘f3’….
F1…….F12键
‘volumemute’, ‘volumedown’, ‘volumeup’
有些键盘没有
‘pause’
PAUSE键
‘capslock’, ‘numlock’, ‘scrolllock’
CAPS LOCK, NUM LOCK, 和 SCROLL LOCK 键
‘insert’
INS或INSERT键
‘printscreen’
PRTSC 或 PRINT SCREEN键
‘winleft’, ‘winright’
Win键
‘command’
Mac OS X command键
文档:
https://muxuezi.github.io/posts/doc-pyautogui.html
http://pyautogui.readthedocs.io/en/latest/introduction.html
http://blog.csdn.net/ibiao/article/details/54406803
本文地址:http://www.chenxm.cc/post/547.html
版权声明:本文为原创文章,版权归 Pala 所有,欢迎分享本文,转载请保留出处!