龙空技术网

python办公自动化:PyAutoGUI入门秘笈二

python小白社区 296

前言:

现时朋友们对“jquerymouseoute”大概比较关怀,小伙伴们都想要分析一些“jquerymouseoute”的相关资讯。那么小编在网上收集了一些对于“jquerymouseoute””的相关知识,希望同学们能喜欢,你们一起来了解一下吧!

接上篇的python办公自动化:PyAutoGUI入门秘笈一后,我们接下来对鼠标控制功能做更深一步的探索。学好鼠标控制,从此游戏,办公任你纵横。鼠标控制功能

鼠标控制功能

屏幕和鼠标位置

屏幕上的位置由X和Y笛卡尔坐标引用。X坐标从左侧的0开始向右增加。与数学不同,Y坐标从顶部的0开始,然后逐渐增加。

0,0 X increases -->+---------------------------+| | Y increases| | || 1920 x 1080 screen | || | V| || |+---------------------------+ 1919, 1079

左上角的像素位于坐标0,0处。如果屏幕的分辨率为1920 x 1080,则右下角的像素为1919,1079(因为坐标从0开始,而不是1)。

屏幕的分辨率

大小由size()函数返回为两个整数的元组。position()函数返回鼠标光标的当前X和Y坐标。

例如:

>>> pyautogui.size()(1920, 1080)>>> pyautogui.position()(187, 567)

这是一个简短的Python 3程序,它将不断打印出鼠标光标的位置:

#! python3import pyautogui, sysprint('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 2版本:

#! pythonimport pyautogui, sysprint('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'

要检查屏幕上是否有XY坐标,请将它们(作为两个整数参数或带有两个整数的单个元组/列表参数)传递给onScreen()函数,True如果它们位于屏幕的边界内,则返回,False如果不是,则返回。例如:

>>> 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

鼠标移动

该moveTo()函数将鼠标光标移动到您传递的X和Y整数坐标。None可以为坐标传递该值以表示“当前鼠标光标位置”。例如:

>>> 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.

通常鼠标光标会立即移动到新坐标。如果您希望鼠标逐渐移动到新位置,请传递第三个参数,持续时间(以秒为单位)。例如:

>>> pyautogui.moveTo(100, 200, 2) # moves mouse to X of 100, Y of 200 over 2 seconds

(如果持续时间小于pyautogui.MINIMUM_DURATION移动将是即时的。默认情况下,pyautogui.MINIMUM_DURATION为0.1。)

如果要将鼠标光标相对于其当前位置移动几个像素,请使用该move()功能。此功能具有与之类似的参数moveTo()。例如:

>>> 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.

鼠标拖动

PyAutoGUI的dragTo()和drag()功能也有类似的参数作为moveTo()和move()功能。此外,他们有一个button可以被设置为关键字'left','middle'以及'right'针对鼠标左键按住拖动。例如:

>>> 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

补间/缓动函数

补间/缓动函数

补间是一个额外的功能,像上图飞机飞过的烟迹一样使鼠标移动花哨。如果您不关心这一点,您可以跳过本节。

补间或缓动函数指示鼠标移动到目标时的进度。通常,当鼠标移动一段时间时,鼠标以恒定速度直线移动到目的地。这称为线性补间或线性缓动函数。

PyAutoGUI具有pyautogui模块中可用的其他补间函数。该pyautogui.easeInQuad功能可通过第四届参数moveTo(),move(),dragTo()和drag()功能来使鼠标光标开始缓慢移动,然后朝着目标加快。总持续时间仍然与传递给函数的参数相同。该pyautogui.easeOutQuad是相反的:把鼠标指针开始移动速度快,但速度减慢,因为它接近目的地。该pyautogui.easeOutElastic直到它稳定在目的地将冲过目标和“橡皮筋”来回。

例如:

>>> 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

这些函数是从Al Sweigart的PyTweening模块复制而来的:https ://pypi.python.org/pypi/PyTweening 不必安装此模块即可使用该功能。

如果要创建自己的补间函数,请定义一个函数,该函数在0.0(表示鼠标移动的开始)和1.0(表示鼠标移动的结束)之间采用单个浮点参数,并在0.0和之间返回浮点值1.0。

鼠标点击

该click()功能模拟鼠标当前位置的单个鼠标左键单击。“点击”定义为按下按钮然后将其释放。例如:

>>> pyautogui.click() # click the mouse

要moveTo()在单击之前组合调用,请传递x和y关键字参数的整数:

>>> pyautogui.click(x=100, y=200) # move to 100, 200, then click the left mouse button.

要指定不同的鼠标键点击,传递'left','middle'或 关键字参数:'right'``for the ``button

>>> pyautogui.click(button='right') # right-click the mouse

要进行多次单击,请将整数传递给clicks关键字参数。(可选)您可以将float或integer传递给intervalkeyword参数,以指定点击之间的暂停量(以秒为单位)。例如:

>>> 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

作为一种方便的快捷方式,该doubleClick()功能将双击鼠标左键。它还具有可选x,y,interval,和button关键字参数。例如:

>>> pyautogui.doubleClick() # perform a left-button double click

还有一个tripleClick()具有类似可选关键字参数的函数。

该rightClick()函数具有可选x和y关键字参数。

mouseDown()和mouseUp()函数

鼠标点击和拖动包括按下鼠标按钮并将其释放回来。如果要单独执行这些操作,请调用mouseDown()和mouseUp()功能。它们具有相同的x,y和button。例如:

>>> 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.

鼠标滚动

可以通过调用scroll()函数并传递整数个“点击”来滚动来模拟鼠标滚轮。“点击”中的滚动量因平台而异。(可选)可以传递整数x和y关键字参数,以便在执行滚动之前移动鼠标光标。例如:

>>> 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"

在OS X和Linux平台上,PyAutoGUI还可以通过调用hscroll()函数来执行水平滚动。例如:

>>> pyautogui.hscroll(10) # scroll right 10 "clicks">>> pyautogui.hscroll(-10) # scroll left 10 "clicks"

该scroll()函数是一个包装器vscroll(),用于执行垂直滚动。

老铁们感觉有收获吗?可以点赞或支持哦,接下来我们进入下一站,键盘控制。

标签: #jquerymouseoute