Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Automation method of PyAutoGUI graphical user Interface

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "PyAutoGUI graphical user interface automation method" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "PyAutoGUI graphical user interface automation method" article.

Before we begin

Before you begin, to install the required module pyautogui, the pyautogui module can send virtual buttons and mouse clicks to Windows.

Pip install-I https://pypi.douban.com/simple pyautogui view screen size

Pyautogui.size () can get the number of pixels (integers) of the width and height of the screen.

> pyautogui.size () Size (width=1920, height=1080) # can also directly save width and height x, y = pyautogui.size () to get the current mouse position

The function returns the tuple of the current position XQuery Y coordinates, and if you set the parameter of xQuery y, the returned result will be overridden.

> pyautogui.position (x=None, y=None) Point (x = 1437, y = 817) checks whether the specified coordinates are on the screen.

To check whether the XQuery Y coordinates are on the screen, pass them (support for two integers, lists, tuples) to the onScreen () function, and return True if they are within the boundaries of the screen, otherwise return False.

> pyautogui.size () (1920, 1080) > pyautogui.onScreen (1920, 1080) False > pyautogui.onScreen (1919, 1079) True setting pause time

Use the pyautogui.PAUSE variable to set the number of seconds to pause. For example, after setting pyautogui.PAUSE = 1, each PyAutoGUI function call waits for a second after performing an action.

> pyautogui.PAUSE = 1 Mouse function Mobile Mouse

Move the mouse to the specified location.

Pyautogui.moveTo (x=None, y=None, duration=0.0) # move the mouse to the specified location

Pyautogui.move (xOffset=None, yOffset=None, duration=0.0) # replaced moveRel in PyAutoGUI 1.0, but moveRel can also use

Pyautogui.moveTo (200,300, duration=0.5) # move to coordinates (200300) pyautogui.move (200,300, duration=0.5) # move 200 to the right from the current mouse position and move 300 down

Main parameters:

X/xOffset:X axis coordinates. Take a value of 0 or a positive integer value in moveTo (), and a negative value in move () / moveRel (), which represents moving to the left at the current position.

Y/yOffset:Y axis coordinates. Take a value of 0 or a positive integer value in moveTo (), and a negative value in move () / moveRel (), which means to move upward in the current position.

Duration: the time it takes to move to the specified coordinates. Default is 0, which means it is completed immediately (in s).

Mouse click

Use click () to complete the mouse click.

> pyautogui.click (x=None, y=None, clicks=1, interval=0.0, button=PRIMARY, duration=0.0)

Main parameters:

XRV X axis coordinates, default is None, that is, the current X axis coordinates

YRV Y-axis coordinates, default is None, that is, the current Y-axis coordinates (Note: the coordinates of the Y-axis can only be specified at the same time, not only one)

Clicks: the number of mouse clicks. Default is 1.

Interval:int or floating point number, indicating the number of seconds to wait between clicks. The default value is 0.0, indicating that there is no pause between clicks.

Button: optional LEFT, MIDDLE, RIGHT, PRIMARY (left button) or SECONDARY (right button). Its default value is PRIMARY

Duration: if you specify the value of XQuery Y, and the coordinate is not the current position of the mouse, then the duration parameter will take effect. It represents the time it takes to move to the specified coordinate. The default is 0, which means to move immediately.

? Other click actions:

Pyautogui.mouseDown () # Press the mouse button (left button) pyautogui.mouseUp () # release the mouse button (left button) Mouse drag

Hold down a key of the mouse and drag.

Pyautogui.dragTo (x=None, y=None, duration=0.0, button=PRIMARY) # drag the mouse to the specified location pyautogui.drag (xOffset=0, yOffset=0, duration=0.0, button=PRIMARY) # drag the mouse to a point on the screen relative to the current position.

Main parameters:

Xminute _ xOffsetreyOffsetrecoveryOffsetdisplacement x and y indicate the location of the mouse event. If None, the current mouse position is used. If it is a floating point value, it is rounded. If it is outside the screen boundary, the event occurs at the edge of the screen.

Duration: if you specify the value of XQuery Y, and the coordinate is not the current position of the mouse, then the duration parameter will take effect. It represents the time it takes to move to the specified coordinate. The default is 0, which means to move immediately.

Button: optional LEFT, MIDDLE, RIGHT, PRIMARY (left button) or SECONDARY (right button). Its default value is PRIMARY

▶️ example:

Mouse scrolling

Scrolling vertically, scrolling on different platforms may have different effects.

> pyautogui.scroll (100) # scroll up 100 > pyautogui.scroll (- 100) # scroll down 100 > pyautogui.scroll (100,100, 200) # move to 200, then scroll

Scroll horizontally (for OS X and Linux platforms).

> pyautogui.hscroll (100) # Scroll 100 to the right > pyautogui.hscroll (- 100) # Scroll 100 to the left

▶️ example:

Screen function screenshot function

Use the screenshot () function to achieve screenshot. A call to screenshot () returns an Image object. Passing in a file name string will save the screenshot to the file and return it as an Image object. To capture the specified area through the region keyword parameter, you only need to pass a tuple of left,top, width, and height (left,top is the upper-left coordinate).

> import pyautogui > pyautogui.screenshot () > pyautogui.screenshot (ringing C:\ Users\ pc\ Desktop\ test.png') > pyautogui.screenshot (ringing C:\ Users\ pc\ Desktop\ test.png', region= (300,300,500, 1000)) location function

If you don't know the exact coordinates of what you want to click on the screen, you cannot call the moveTo () and click () functions. However, if you have an image of the button, such as the image below, you can call the locateOnScreen ('image path') function to get the coordinates. The return value is a tuple: (left, top, width, height). This tuple can be passed to center () to get the coordinates of the center of the region (XPerry Y). If no image is found on the screen, locateOnScreen () throws an ImageNotFoundException exception.

> location = pyautogui.locateOnScreen ('picture path') > pyautogui.center (location)

You can also pass grayscale=True to locateOnScreen to speed up slightly (about 30%). This reduces the color saturation of images and screenshots and speeds up positioning, but may lead to mismatching.

Message box function

PyAutoGUI uses the message box function in PyMsgBox to provide a cross-platform pure Python method to display javascript-style message boxes.

Alert () function > pyautogui.alert (text='text', title='title', alerts') 'alerts'

Displays a simple message box with text and an alert! Button. Returns the value of button after clicking the button.

▶️ example:

Confirm () function

Displays a message box with multiple buttons. The number and text of buttons can be set by yourself. Click the button to return to the text of the button.

> pyautogui.confirm (text='text', title='title', buttons= ['think again', 'uninstall']) 'uninstall'

▶️ example:

Prompt () function

A message box containing confirmation, cancel buttons and text input fields is displayed, and the user can enter the specified content. When the confirm button is clicked, the value in the input box is returned; if you click cancel, the value is returned to None.

Pyautogui.prompt (text='text', title='title', default=', please enter text message')

▶️ example:

Password () function

Displays a message box containing confirmation, cancel buttons and text input fields, and the characters entered are displayed as "*". If you click OK, you will return the entered text; if you click cancel, it will be None.

Pyautogui.password (text='text', title='title', default=' password', mask='*')

▶️ example:

Keyboard control function write () function

The write () function types characters into the string passed in. To add a delay interval between each character key pressed, the interval parameter sets the delay between each key.

> pyautogui.write ('Hello worldview, interval=0.25) # each character has an interval of 0.25 seconds

Note: pyautogui does not support autofocus of the input box. Click on the position of the input box before entering.

Press () function

The press () function performs the same operation as the click () of a mouse operation, receiving the string passed to it.

Press ('num0', presses=1, interval=0.0)

Keys: keyboard string passed in

Presses: number of keystrokes to operate. Default is once.

Interval: interval between keystrokes. Default is 0.

All values of keys are as follows:

[', '7,'8, 9,':,' ','','?','@','[','\',']','^','_','`,'a baby, 'baked,' caged, 'dashed,' faded, 'grubbed,' hacked, 'ified,' jacked, 'kwon,' lumped, 'milled,' nicked, 'oiled,' paired,'q' '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 'final',' fn', 'hanguel',' hangul','hanja', 'home',' insert', 'junja',' 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'] hotkey () function

Receive multiple string parameters, press them in order, and then release them in reverse order.

Pyautogui.hotkey ('ctrl',' shift', 'esc') is equivalent to pyautogui.keyDown (' ctrl') # press ctrl pyautogui.keyDown ('shift') # press shiftpyautogui.keyDown (' esc') # press escpyautogui.keyUp ('esc') # release ctrl pyautogui.keyUp (' shift') # release shiftpyautogui.keyUp ('ctrl') # release esc

▶️ example:

The above is the content of this article on "methods of PyAutoGUI graphical user interface automation". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report