用户:Leo腾查看:6 回复:3 评论:6 创建时间:2019-11-18T20:44:49
部分内容参考互联网:
https://pyautogui.readthedocs.io/en/latest/
https://blog.csdn.net/qq_38839677/article/details/83374087
https://blog.csdn.net/Zhang_Chen_/article/details/88751605
https://www.cnblogs.com/zwc--blog/p/11079986.html
大家好,我是Leo腾TCE。
经过我的一番查找,老夫掐指一算,目前编程猫社区上还没有和Pyautogui有关的任何教程帖。
那么,作为第一个吃螃蟹,啊不对,是吃Pyautogui的人,Pyautogui教程帖这个重任就交给我吧!
——————分割线——————
一、Pyautogui简介
首先,我们要知道,什么是Pyautogui。
官方是这样介绍Pyautogui的:
The purpose of PyAutoGUI is to provide a cross-platform Python module for GUI automation for human beings. The API is designed to be as simple as possible with sensible defaults.
PyAutoGUI的目的是为人类的GUI自动化提供一个跨平台的Python模块。该API设计为使用合理的默认设置尽可能简单。
PyAutoGUI can simulate moving the mouse, clicking the mouse, dragging with the mouse, pressing keys, pressing and holding keys, and pressing keyboard hotkey combinations.
PyAutoGUI可以模拟移动鼠标,单击鼠标,用鼠标拖动,按下键,按住键以及按下键盘热键组合的过程。
正如上面所述,Pyautogui可以完成鼠标和键盘的大部分操作。
首先,在程序中使用Pyautogui的时候,建议大家加上这样几句:
import pyautogui
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 2.5
为什么要这样做呢?官方对此是这样解释的:
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.
作为安全功能,默认情况下启用故障安全功能。当PyAutoGUI功能将引发如果鼠标光标在屏幕的左上角。如果您失去控制并且需要停止当前的PyAutoGUI功能,请继续将鼠标光标向上和向左移动。
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.
您可以在PyAutoGUI的所有功能之后添加延迟,方法是将该pyautogui.PAUSE变量设置为浮点数或要暂停的秒数的整数值。默认情况下,暂停设置为0.1秒。与其他应用程序交互时,这可能会很有帮助,这样PyAutoGUI不会对它们移动太快。
所以,各位应该都能理解了吧?(或者看得头昏眼花?)
——————分割线——————
二、Pyautogui下载
这个……真是为难了用海龟编辑器的我啊。
在海龟编辑器上,我们要这么下载它:
库管理->搜索Pyautogui->下载
——————分割线——————
三、Pyautogui操作
1.一般功能
pyautogui.position() #返回整数元组:(x,y)表示鼠标光标的当前位置。
pyautogui.size() #返回整数元组:(宽度,高度)表示主监视器的大小。
2.鼠标控制功能
首先,屏幕上的位置由X和Y直角坐标表示。X坐标从左侧的0开始,向右增加。与数学不同,Y坐标从顶部的0开始,向下增加。
下面是官方发的一张图:
0,0 X 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).
左上角的像素位于坐标0,0。如果屏幕的分辨率为1920*1080,则右下角的像素将为1919,1079
(因为坐标始于0,而不是1)。
希望你们能看懂吧,看不懂我也无能为力了。
如果要检查鼠标是否在屏幕之内,需要用到onScreen()函数,如果参数中的列表/元组所代表的坐标在屏幕之内,则返回True、否则返回false:
#设显示器的分辨率为1920*1080
pyautogui.onScreen(1920, 1080) #False
pyautogui.onScreen(1919, 1079) #True
(1)鼠标移动
pyautogui.moveTo(100,100) #将鼠标移到X:100,Y:100的位置
pyautogui.moveTo(100,150,2) #在2秒内将鼠标移到X:100,Y:150的位置
pyautogui.moveTo(None,200) #将鼠标移到X:原来的X坐标,Y:200的位置
pyautogui.move(100,100) #将鼠标在原来的基础上X坐标增加100,Y坐标增加100
pyautogui.move(None,100) #将鼠标在原来的基础上Y坐标增加100
(2)鼠标拖曳
pyautogui.dragTo(100, 200, button='left') #按住鼠标左键,拖动鼠标到X:100,Y:200
pyautogui.dragTo(300, 400, 2, button='left') #按住鼠标左键,在2秒内移到X:300,Y:400
pyautogui.drag(30, 0, 2, button='right') #按住鼠标右键,在2秒内移到X:30,Y:0
(3)鼠标点击
pyautogui.click(x=100, y=100, botton='right', clicks=3, interval=0.25)
#将鼠标移动到X:100,Y:100后,点击右键(也可以为left左键)3次,间隔0.25
pyautogui.mouseDown()
pyautogui.mouseUp()
#上面的过程相当于pyautogui.click()
(4)鼠标滚动
pyautogui.scroll(10) #鼠标向上滚动10
pyautogui.scroll(-10) #鼠标向下滚动10
pyautogui.scroll(10, x=100, y=100) #将鼠标移到X:100,Y:100后向上滚动10
——————分割线——————
好的,那么本次的教学就到这里结束了,后续会为大家带来Pyautogui的键盘控制功能教学
如果想要看到后续内容,欢迎来论坛搜索【TCE的编程小讲堂】关键词找我哦!
本人是学生党,用上学时间赶出来的帖子,如果有不详细之处还望谅解
如果认为更新速度太慢的话,也可以搜索https://pyautogui.readthedocs.io/en/latest/快速学习哟!

过了这么久回来看一下,发现我居然打错了55
pyautogui.drag(30, 0, 2, button='right') #按住鼠标右键,在2秒内使鼠标的x坐标在原来的基础上增加30,y坐标不变(增加0)点赞0
评论