049-python包pyautogui
一、鼠标键盘
pyautogui官方文档
1、鼠标移动到屏幕正中间示例
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
2、常用操作
- 获取屏幕宽高
screenWidth, screenHeight = pyautogui.size()
- 获取当前鼠标位置
currentMouseX, currentMouseY = pyautogui.position()
- 鼠标移动到绝对位置
pyautogui.moveTo(100, 150)
- 鼠标点击
pyautogui.click()
- 鼠标相对移动。鼠标下移10像素
pyautogui.moveRel(None, 10)
- 鼠标双击
pyautogui.doubleClick()
- 鼠标带动画和时间移动。2秒用easeInOut动画方式移动到指定位置。
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)
- 键盘打字。耗时0.25秒。
pyautogui.typewrite('Hello world!', interval=0.25)
- 按键esc
pyautogui.press('esc')
- 按下按键shift
pyautogui.keyDown('shift')
- 连续按键left
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])
- 弹起按键shift
pyautogui.keyUp('shift')
- 热键
pyautogui.hotkey('ctrl', 'c')
3、一个鼠标拖拽移动示例。
文章图片
image.png
import pyautogui,time
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
distance = 200
while distance > 0:
pyautogui.dragRel(distance, 0, duration=0.5)# move right
distance -= 5
pyautogui.dragRel(0, distance, duration=0.5)# move down
pyautogui.dragRel(-distance, 0, duration=0.5)# move left
distance -= 5
pyautogui.dragRel(0, -distance, duration=0.5)# move up
二、鼠标控制功能(Mouse Control Functions)
- The Screen and Mouse Position(屏幕和鼠标位置)
0,0X increases -->
+---------------------------+
|| Y increases
|||
|1920 x 1080 screen||
||V
||
||
+---------------------------+ 1919, 1079
The pixel at the top-left corner is at coordinates 0, 0. If your screen’s resolution is 1920 x 1080, the pixel in the lower right corner will be 1919, 1079 (since the coordinates begin at 0, not 1).
如果屏幕分辨率是1920*1080,则屏幕值最大到1919,1079因为是从0开始的。
The screen resolution size is returned by the size() function as a tuple of two integers. The current X and Y coordinates of the mouse cursor are returned by the position() function.
For example:
>>> pyautogui.size()
(1920, 1080)
>>> pyautogui.position()
(187, 567)
size() 功能返回两个整数。position()功能也返回两个整数。
Here is a short Python 3 program that will constantly print out the position of the mouse cursor:
#! python3
import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')
这里是一个简短的Python 3程序,将不断地打印出鼠标光标的位置。
Here is the Python 2 version:
#! python
import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print positionStr,
print '\b' * (len(positionStr) + 2),
sys.stdout.flush()
except KeyboardInterrupt:
print '\n'
Python 2下,不断地打印出鼠标光标的位置。
To check if XY coordinates are on the screen, pass them (either as two integer arguments or a single tuple/list arguments with two integers) to the onScreen() function, which will return True if they are within the screen’s boundaries and False if not. For example:
>>> pyautogui.onScreen(0, 0)
True
>>> pyautogui.onScreen(0, -1)
False
>>> pyautogui.onScreen(0, 99999999)
False
>>> pyautogui.size()
(1920, 1080)
>>> pyautogui.onScreen(1920, 1080)
False
>>> pyautogui.onScreen(1919, 1079)
True
是否在屏幕上示例。
- Mouse Movement(鼠标事件)
>>> pyautogui.moveTo(100, 200)# moves mouse to X of 100, Y of 200.
>>> pyautogui.moveTo(None, 500)# moves mouse to X of 100, Y of 500.
>>> pyautogui.moveTo(600, None)# moves mouse to X of 600, Y of 500.
鼠标移动示例。
Normally the mouse cursor will instantly move to the new coordinates. If you want the mouse to gradually move to the new location, pass a third argument for the duration (in seconds) the movement should take. For example:
>>> pyautogui.moveTo(100, 200, 2)# moves mouse to X of 100, Y of 200 over 2 seconds
(If the duration is less than pyautogui.MINIMUM_DURATION the movement will be instant. By default, pyautogui.MINIMUM_DURATION is 0.1.)
If you want to move the mouse cursor over a few pixels relative to its current position, use the move() function. This function has similar parameters as moveTo(). For example:
>>> pyautogui.moveTo(100, 200)# moves mouse to X of 100, Y of 200.
>>> pyautogui.move(0, 50)# move the mouse down 50 pixels.
>>> pyautogui.move(-30, 0)# move the mouse left 30 pixels.
>>> pyautogui.move(-30, None)# move the mouse left 30 pixels.
默认间隔0.1秒。moveTo()是绝对移动。move()是相对移动。
- Mouse Drags(鼠标拖拽)
>>> pyautogui.dragTo(100, 200, button='left')# drag mouse to X of 100, Y of 200 while holding down left mouse button
>>> pyautogui.dragTo(300, 400, 2, button='left')# drag mouse to X of 300, Y of 400 over 2 seconds while holding down left mouse button
>>> pyautogui.drag(30, 0, 2, button='right')# drag the mouse left 30 pixels over 2 seconds while holding down the right mouse button
鼠标拖拽示例。
- Tween / Easing Functions(移动动画)(渐变/宽松动画)
A tween or easing function dictates the progress of the mouse as it moves to its destination. Normally when moving the mouse over a duration of time, the mouse moves directly towards the destination in a straight line at a constant speed. This is known as a linear tween or linear easing function.
PyAutoGUI has other tweening functions available in the pyautogui module. The pyautogui.easeInQuad function can be passed for the 4th argument to moveTo(), move(), dragTo(), and drag() functions to have the mouse cursor start off moving slowly and then speeding up towards the destination. The total duration is still the same as the argument passed to the function. The pyautogui.easeOutQuad is the reverse: the mouse cursor starts moving fast but slows down as it approaches the destination. The pyautogui.easeOutElastic will overshoot the destination and “rubber band” back and forth until it settles at the destination.
For example:
>>> 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.easeInQuad是先慢后快动画。pyautogui.easeOutQuad是先快后慢动画。
pyautogui.easeOutElastic是橡皮筋动画。
pyautogui.easeInOutQuad是最好用好看的动画。
These tweening functions are copied from Al Sweigart’s PyTweening module: https://pypi.python.org/pypi/PyTweening https://github.com/asweigart/pytweening This module does not have to be installed to use the tweening functions.
If you want to create your own tweening function, define a function that takes a single float argument between
0.0
(representing the start of the mouse travelling) and 1.0
(representing the end of the mouse travelling) and returns a float value between 0.0
and 1.0
.可以自定义动画的。
- Mouse Clicks(鼠标点击)
>>> pyautogui.click()# click the mouse
To combine a moveTo() call before the click, pass integers for the x and y keyword argument:
>>> pyautogui.click(x=100, y=200)# move to 100, 200, then click the left mouse button.
鼠标点击,和点击指定位置示例。
To specify a different mouse button to click, pass 'left', 'middle', or 'right'
for the
button keyword argument:>>> pyautogui.click(button='right')# right-click the mouse
右击示例。
【049-python包pyautogui】To do multiple clicks, pass an integer to the clicks keyword argument. Optionally, you can pass a float or integer to the interval keyword argument to specify the amount of pause between the clicks in seconds. For example:
>>> pyautogui.click(clicks=2)# double-click the left mouse button
>>> pyautogui.click(clicks=2, interval=0.25)# double-click the left mouse button, but with a quarter second pause in between clicks
>>> pyautogui.click(button='right', clicks=3, interval=0.25)## triple-click the right mouse button with a quarter second pause in between clicks
As a convenient shortcut, the doubleClick() function will perform a double click of the left mouse button. It also has the optional x, y, interval, and button keyword arguments. For example:
>>> pyautogui.doubleClick()# perform a left-button double click
There is also a tripleClick() function with similar optional keyword arguments.
The rightClick() function has optional x and y keyword arguments.
鼠标双击示例如上。
- The mouseDown() and mouseUp() Functions(鼠标按下弹起功能)
>>> pyautogui.mouseDown();
pyautogui.mouseUp()# does the same thing as a left-button mouse click
>>> pyautogui.mouseDown(button='right')# press the right button down
>>> pyautogui.mouseUp(button='right', x=100, y=200)# move the mouse to 100, 200, then release the right button up.
鼠标按下弹起功能示例。
- Mouse Scrolling(鼠标滚动,滚鼠)
>>> pyautogui.scroll(10)# scroll up 10 "clicks"
>>> pyautogui.scroll(-10)# scroll down 10 "clicks"
>>> pyautogui.scroll(10, x=100, y=100)# move mouse cursor to 100, 200, then scroll up 10 "clicks"
滚鼠示例。
On OS X and Linux platforms, PyAutoGUI can also perform horizontal scrolling by calling the hscroll() function. For example:
>>> pyautogui.hscroll(10)# scroll right 10 "clicks"
>>> pyautogui.hscroll(-10)# scroll left 10 "clicks"
The scroll() function is a wrapper for vscroll(), which performs vertical scrolling.
Mac和Linux下,示例。需要调用hscroll()或者vscroll()。水平或竖直移动。
三、键盘控制功能(Keyboard Control Functions)
- The typewrite() Function(打字输入功能)
For example:
>>> pyautogui.typewrite('Hello world!')# prints out "Hello world!" instantly
>>> pyautogui.typewrite('Hello world!', interval=0.25)# prints out "Hello world!" with a quarter second delay after each character
You can only press single-character keys with typewrite(), so you can’t press the Shift or F1 keys, for example.
打字功能示例。
- The press(), keyDown(), and keyUp() Functions(按键,按下和弹起功能)
press()
function and pass it a string from the pyautogui.KEYBOARD_KEYS
such as enter
, esc
, f1
. See KEYBOARD_KEYS.For example:
>>> pyautogui.press('enter')# press the Enter key
>>> pyautogui.press('f1')# press the F1 key
>>> pyautogui.press('left')# press the left arrow key
The press() function is really just a wrapper for the keyDown() and keyUp() functions, which simulate pressing a key down and then releasing it up. These functions can be called by themselves. For example, to press the left arrow key three times while holding down the Shift key, call the following:
>>> pyautogui.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
To press multiple keys similar to what typewrite() does, pass a list of strings to press(). For example:
>>> pyautogui.press(['left', 'left', 'left'])
按键,按下和弹起功能示例。
- The hotkey() Function(热键功能)
>>> pyautogui.hotkey('ctrl', 'shift', 'esc')
…is equivalent to this code:
>>> pyautogui.keyDown('ctrl')
>>> pyautogui.keyDown('shift')
>>> pyautogui.keyDown('esc')
>>> pyautogui.keyUp('esc')
>>> pyautogui.keyUp('shift')
>>> pyautogui.keyUp('ctrl')
热键功能简单好用,上例1等效例2。
- KEYBOARD_KEYS(按键)
The following are the valid strings to pass to the press(), keyDown(), keyUp(), and hotkey() functions:
['\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', "'", '(',
')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';
', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace',
'browserback', 'browserfavorites', 'browserforward', 'browserhome',
'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear',
'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete',
'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',
'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja',
'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail',
'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',
'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6',
'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn',
'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn',
'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator',
'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',
'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',
'command', 'option', 'optionleft', 'optionright']
能按的键列表。
四、消息弹框功能(Message Box Functions) PyAutoGUI makes use of the message box functions in PyMsgBox to provide a cross-platform, pure Python way to display JavaScript-style message boxes. There are four message box functions provided:
提供的消息弹框功能样式和JavaScript差不多。
- The alert() Function(警告框功能)
>>> alert(text='', title='', button='OK')
Displays a simple message box with text and a single OK button. Returns the text of the button clicked on.
简单的消息框,就一个OK确认按钮。
- The confirm() Function(确认框功能)
>>> confirm(text='', title='', buttons=['OK', 'Cancel'])
Displays a message box with OK and Cancel buttons. Number and text of buttons can be customized. Returns the text of the button clicked on.
能自定义。
- The prompt() Function(提示框功能)
>>> prompt(text='', title='' , default='')
Displays a message box with text input, and OK & Cancel buttons. Returns the text entered, or None if Cancel was clicked.
可以输入内容。
- The password() Function(密码框功能)
>>> password(text='', title='', default='', mask='*')
Displays a message box with text input, and OK & Cancel buttons. Typed characters appear as *. Returns the text entered, or None if Cancel was clicked.
密码会以*显示。
五、截图功能(Screenshot Functions) PyAutoGUI can take screenshots, save them to files, and locate images within the screen. This is useful if you have a small image of, say, a button that needs to be clicked and want to locate it on the screen. These features are provided by the PyScreeze module, which is installed with PyAutoGUI.
Screenshot functionality requires the Pillow module. OS X uses the screencapture command, which comes with the operating system. Linux uses the scrot command, which can be installed by running sudo apt-get install scrot.
可以截图,保存图片,找图。
- Special Notes About Ubuntu(Ubuntu下看看)
- The screenshot() Function(截图功能)
>>> import pyautogui
>>> im1 = pyautogui.screenshot()
>>> im2 = pyautogui.screenshot('my_screenshot.png')
On a 1920 x 1080 screen, the screenshot() function takes roughly 100 milliseconds - it’s not fast but it’s not slow.
There is also an optional region keyword argument, if you do not want a screenshot of the entire screen. You can pass a four-integer tuple of the left, top, width, and height of the region to capture:
>>> import pyautogui
>>> im = pyautogui.screenshot(region=(0,0, 300, 400))
截图功能示例,区域截图示例。
- The Locate Functions(找图功能)
一个版本问题。
You can visually locate something on the screen if you have an image file of it. For example, say the calculator app was running on your computer and looked like this:
文章图片
image.png You can’t call the moveTo() and click() functions if you don’t know the exact screen coordinates of where the calculator buttons are. The calculator can appear in a slightly different place each time it is launched, causing you to re-find the coordinates each time. However, if you have an image of the button, such as the image of the 7 button:
文章图片
image.png … you can call the locateOnScreen('calc7key.png') function to get the screen coordinates. The return value is a 4-integer tuple: (left, top, width, height). This tuple can be passed to center() to get the X and Y coordinates at the center of this region. If the image can’t be found on the screen, locateOnScreen() raises ImageNotFoundException.
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png')
>>> button7location
Box(left=1416, top=562, width=50, height=41)
>>> button7location[0]
1416
>>> button7location.left
1416
>>> button7point = pyautogui.center(button7location)
>>> button7point
Point(x=1441, y=582)
>>> button7point[0]
1441
>>> button7point.x
1441
>>> button7x, button7y = button7point
>>> pyautogui.click(button7x, button7y)# clicks the center of where the 7 button was found
>>> pyautogui.click('calc7key.png') # a shortcut version to click on the center of where the 7 button was found
计算器找图功能示例。
The optional confidence keyword argument specifies the accuracy with which the function should locate the image on screen. This is helpful in case the function is not able to locate an image due to negligible pixel differences:
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png', confidence=0.9)
>>> button7location
Box(left=1416, top=562, width=50, height=41)
The locateCenterOnScreen() function combines locateOnScreen() and center():
>>> import pyautogui
>>> x, y = pyautogui.locateCenterOnScreen('calc7key.png')
>>> pyautogui.click(x, y)
On a 1920 x 1080 screen, the locate function calls take about 1 or 2 seconds. This may be too slow for action video games, but works for most purposes and applications.
There are several “locate” functions. They all start looking at the top-left corner of the screen (or image) and look to the right and then down. The arguments can either be a
- locateOnScreen(image, grayscale=False) - Returns (left, top, width, height) coordinate of first found instance of the image on the screen. Raises ImageNotFoundException if not found on the screen.
- locateCenterOnScreen(image, grayscale=False) - Returns (x, y) coordinates of the center of the first found instance of the image on the screen. Raises ImageNotFoundException if not found on the screen.
- locateAllOnScreen(image, grayscale=False) - Returns a generator that yields (left, top, width, height) tuples for where the image is found on the screen.
- locate(needleImage, haystackImage, grayscale=False) - Returns (left, top, width, height) coordinate of first found instance of needleImage in haystackImage. Raises ImageNotFoundException if not found on the screen.
- locateAll(needleImage, haystackImage, grayscale=False) - Returns a generator that yields (left, top, width, height) tuples for where needleImage is found in haystackImage.
>>> import pyautogui
>>> for pos in pyautogui.locateAllOnScreen('someButton.png')
...print(pos)
...
(1101, 252, 50, 50)
(59, 481, 50, 50)
(1395, 640, 50, 50)
(1838, 676, 50, 50)
>>> list(pyautogui.locateAllOnScreen('someButton.png'))
[(1101, 252, 50, 50), (59, 481, 50, 50), (1395, 640, 50, 50), (1838, 676, 50, 50)]
These “locate” functions are fairly expensive; they can take a full second to run. The best way to speed them up is to pass a region argument (a 4-integer tuple of (left, top, width, height)) to only search a smaller region of the screen instead of the full screen:
>>> import pyautogui
>>> pyautogui.locateOnScreen('someButton.png', region=(0,0, 300, 400))
找图示例,亲测成功,可以的。
- Grayscale Matching(灰度匹配)
>>> import pyautogui
>>> button7location = pyautogui.locateOnScreen('calc7key.png', grayscale=True)
>>> button7location
(1416, 562, 50, 41)
灰度匹配可以找图加速大约30%。从冲淡的颜色中找,能加速但也可能找不到了。
- Pixel Matching(找色)
>>> import pyautogui
>>> im = pyautogui.screenshot()
>>> im.getpixel((100, 200))
(130, 135, 144)
Or as a single function, call the pixel() PyAutoGUI function, which is a wrapper for the previous calls:
>>> import pyautogui
>>> pix = pyautogui.pixel(100, 200)
>>> pix
RGB(red=130, green=135, blue=144)
>>> pix[0]
130
>>> pix.red
130
If you just need to verify that a single pixel matches a given pixel, call the pixelMatchesColor() function, passing it the X coordinate, Y coordinate, and RGB tuple of the color it represents:
>>> import pyautogui
>>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144))
True
>>> pyautogui.pixelMatchesColor(100, 200, (0, 0, 0))
False
The optional tolerance keyword argument specifies how much each of the red, green, and blue values can vary while still matching:
>>> import pyautogui
>>> pyautogui.pixelMatchesColor(100, 200, (130, 135, 144))
True
>>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134))
False
>>> pyautogui.pixelMatchesColor(100, 200, (140, 125, 134), tolerance=10)
True
找色示例以及颜色匹配示例。
附:依赖 On Windows, PyAutoGUI has no dependencies (other than Pillow and some other modules, which are installed by pip along with PyAutoGUI). It does not need the pywin32 module installed since it uses Python’s own ctypes module.
Windows下,完全不依赖其他内容。pywin32也不需要。
On OS X, PyAutoGUI requires PyObjC installed for the AppKit and Quartz modules. The module names on PyPI to install are
pyobjc-core
and pyobjc
(in that order).Mac下,需要AppKit和Quartz模块。所以需要安装
pyobjc-core
和pyobjc
。On Linux, PyAutoGUI requires python-xlib (for Python 2) or python3-Xlib (for Python 3) module installed.
Linux下,Python 2需要安装
python-xlib
,Python 3需要安装python3-Xlib
。附2:强制中止 Like the enchanted brooms from the Sorcerer’s Apprentice programmed to keep filling (and then overfilling) the bath with water, your program could get out of control (even though it is following your instructions) and need to be stopped. This can be difficult to do if the mouse is moving around on its own, preventing you from clicking on the program’s window to close it down.
程序在跑的过程中,如果鼠标一直在移动,会导致你想中止程序时鼠标根本无法手动移动。所以这是一个需要解决的问题。
As a safety feature, a fail-safe feature is enabled by default. When pyautogui.FAILSAFE = True PyAutoGUI functions will raise a pyautogui.FailSafeException if the mouse cursor is in the upper left corner of the screen. If you lose control and need to stop the current PyAutoGUI function, keep moving the mouse cursor up and to the left. To disable this feature, set FAILSAFE to False:
import pyautogui
pyautogui.FAILSAFE = False # disables the fail-safe
作为安全特性,安全中止默认是打开的。如果需要关闭安全特性。上面的代码就行,FAILSAFE置为False。当pyautogui.FAILSAFE = True时,安全异常会在鼠标移动到最左上角时启动。也就说默认左上角就是关闭(本质是调用pyautogui.FailSafeException异常)程序。
You can add delays after all of PyAutoGUI’s functions by setting the pyautogui.PAUSE variable to a float or integer value of the number of seconds to pause. By default, the pause is set to 0.1 seconds. This can be helpful when interacting with other applications so that PyAutoGUI doesn’t move too fast for them. For example:
import pyautogui
pyautogui.PAUSE = 2.5
pyautogui.moveTo(100, 100);
pyautogui.click()# there will be a two and a half second pause after moving and another after the click
移动和点击操作,默认停止时间设置可以使用pyautogui.PAUSE,如上所示。如果不设置,默认是0.1秒。这样防止脚本执行太快从而无法打断。
All PyAutoGUI functions will block until they complete. (It is on the roadmap to add an optional non-blocking way to call these functions.)
所有PyAutoGUI函数将阻塞,直到他们完成。
It is advised to use FAILSAFE along with setting PAUSE.
pyautogui的作者建议我们设置pyautogui.PAUSE,并打开pyautogui.FAILSAFE,方便随时终止程序。
附3:安装 To install PyAutoGUI, install the pyautogui package from PyPI and dependencies.
On Windows, this is:
C:\Python34\pip.exe install pyautogui
Windows下,这样安装就行。
On OS X, this is:
pip3 install pyobjc-corepip3 install pyobjcpip3 install pyautogui
If you are running El Capitan and have problems installing pyobjc try:
MACOSX_DEPLOYMENT_TARGET=10.11 pip install pyobjc
Mac下,这样安装
On Linux, this is:
pip3 install python3-xlibsudo apt-get install scrotsudo apt-get install python3-tksudo apt-get install python3-devpip3 install pyautogui
Linux下,这样安装。
PyAutoGUI will try to install Pillow (for its screenshot capabilities). This happens when pip installs PyAutoGUI.
用命令pip installs PyAutoGUI时,会试着装Pillow,拥有截图功能。
附4:备忘单(Cheat Sheet) This is a quickstart reference to using PyAutoGUI. PyAutoGUI is cross-platform GUI automation module that works on Python 2 & 3. You can control the mouse and keyboard as well as perform basic image recognition to automate tasks on your computer.
本库Python 2 & 3都能使用。你可以在计算机上控制鼠标和键盘,以及执行基本的图像识别自动化任务。
All the keyword arguments in the examples on this page are optional.
import pyautogui
- General Functions(通用函数)
>>> pyautogui.position()# current mouse x and y
(968, 56)
>>> pyautogui.size()# current screen resolution width and height
(1920, 1080)
>>> pyautogui.onScreen(x, y)# True if x & y are within the screen.
True
- Fail-Safes(执行间隔)
>>> import pyautogui
>>> pyautogui.PAUSE = 2.5
When fail-safe mode is True, moving the mouse to the upper-left will raise a pyautogui.FailSafeException that can abort your program:
>>> import pyautogui
>>> pyautogui.FAILSAFE = True
- Mouse Functions(鼠标功能)
>>> pyautogui.moveTo(x, y, duration=num_seconds)# move mouse to XY coordinates over num_second seconds
>>> pyautogui.moveRel(xOffset, yOffset, duration=num_seconds)# move mouse relative to its current position
左上角是0,0,往右是x增加,往下是y增加。
If duration is 0 or unspecified, movement is immediate. Note: dragging on Mac can’t be immediate.
>>> pyautogui.dragTo(x, y, duration=num_seconds)# drag mouse to XY
>>> pyautogui.dragRel(xOffset, yOffset, duration=num_seconds)# drag mouse relative to its current position
如果duration参数不填或者0,会立马执行代码。在mac下拖动可能不会立马执行。
Calling click() just clicks the mouse once with the left button at the mouse’s current location, but the keyword arguments can change that:
>>> pyautogui.click(x=moveToX, y=moveToY, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
调用click()只是鼠标左键单击,需要更复杂的操作,如上所示。
The button keyword argument can be 'left', 'middle', or 'right'.
button的值可以填
left
,middle
,right
。All clicks can be done with click(), but these functions exist for readability. Keyword args are optional:
>>> pyautogui.rightClick(x=moveToX, y=moveToY)
>>> pyautogui.middleClick(x=moveToX, y=moveToY)
>>> pyautogui.doubleClick(x=moveToX, y=moveToY)
>>> pyautogui.tripleClick(x=moveToX, y=moveToY)
也可以这样调用。
Positive scrolling will scroll up, negative scrolling will scroll down:
>>> pyautogui.scroll(amount_to_scroll, x=moveToX, y=moveToY)
鼠标滚动。正向向上滚,负的向下滚。
Individual button down and up events can be called separately:
>>> pyautogui.mouseDown(x=moveToX, y=moveToY, button='left')
>>> pyautogui.mouseUp(x=moveToX, y=moveToY, button='left')
鼠标按下,弹起。
- Keyboard Functions(键盘功能)
>>> pyautogui.typewrite('Hello world!\n', interval=secs_between_keys)# useful for entering text, newline is Enter
在光标位置输入内容。
A list of key names can be passed too:
>>> pyautogui.typewrite(['a', 'b', 'c', 'left', 'backspace', 'enter', 'f1'], interval=secs_between_keys)
可以通过列表输入。可以指定输入间隔时间。
The full list of key names is in pyautogui.KEYBOARD_KEYS.
可以调用pyautogui.KEYBOARD_KEYS查看键盘列表。
Keyboard hotkeys like Ctrl-S or Ctrl-Shift-1 can be done by passing a list of key names to hotkey():
>>> pyautogui.hotkey('ctrl', 'c')# ctrl-c to copy
>>> pyautogui.hotkey('ctrl', 'v')# ctrl-v to paste
组合键使用示例。
Individual button down and up events can be called separately:
>>> pyautogui.keyDown(key_name)
>>> pyautogui.keyUp(key_name)
按下弹起分开调用使用。
- Message Box Functions(消息框功能)
>>> 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.'
The prompt() function will return None if the user clicked Cancel.
弹框,类似JavaScript。
如果用户点击取消,prompt()功能会返回空。
- Screenshot Functions(截图功能)
On Linux, you must run sudo apt-get install scrot to use the screenshot features.
>>> pyautogui.screenshot()# returns a Pillow/PIL Image object>>> pyautogui.screenshot('foo.png')# returns a Pillow/PIL Image object, and saves it to a file
截图功能示例。底层是使用Pillow/PIL调用图片数据。
Linux下,需要运行sudo apt-get install scrot才能使用截图功能。
If you have an image file of something you want to click on, you can find it on the screen with locateOnScreen().
>>> pyautogui.locateOnScreen('looksLikeThis.png')# returns (left, top, width, height) of first place it is found
(863, 417, 70, 13)
找图。
The locateAllOnScreen() function will return a generator for all the locations it is found on the screen:
>>> for i in pyautogui.locateAllOnScreen('looksLikeThis.png')
...
...
(863, 117, 70, 13)
(623, 137, 70, 13)
(853, 577, 70, 13)
(883, 617, 70, 13)
(973, 657, 70, 13)
(933, 877, 70, 13)
>>> list(pyautogui.locateAllOnScreen('looksLikeThis.png'))
[(863, 117, 70, 13), (623, 137, 70, 13), (853, 577, 70, 13), (883, 617, 70, 13), (973, 657, 70, 13), (933, 877, 70, 13)]
locateAllOnScreen()功能会返回所有位置。示例如上。
The locateCenterOnScreen() function just returns the XY coordinates of the middle of where the image is found on the screen:
>>> pyautogui.locateCenterOnScreen('looksLikeThis.png')# returns center x and y
(898, 423)
locateCenterOnScreen()可以返回找图的中心点值。
These functions return None if the image couldn’t be found on the screen.
找不到图就返回空。
Note: The locate functions are slow and can take a full second or two.
注:找图功能很慢,可能需要一两秒。
推荐阅读
- 喂,你结婚我给你随了个红包
- CET4听力微技能一
- 放下心中的偶像包袱吧
- 社保代缴公司服务费包含哪些
- Beego打包部署到Linux
- 世界之大,包罗万象--|世界之大,包罗万象-- 读《我不过低配的人生》
- 用npm发布一个包的教程并编写一个vue的插件发布
- 积极探索|积极探索 绽放生命 ???——心心相印计划:青少年心理工作研讨小组全国大型公益行动第二次活动包头市青山区分校圆满成功
- 那个喝大了的女人在群里发了一晚上的红包
- HttpClient对外部网络的操作