pyautogui 鼠标键盘自动化 库的中文版
旧电脑键盘做成鼠标垫 #生活技巧# #环保生活技巧# #废物利用#
PyAutoGUI——让所有GUI都自动化
原文:
https://pyautogui.readthedocs.io/en/latest/
,Python自动化工具,更适合处理GUI任务,网页任务推荐:
Selenium+Firefox记录(Chromedriver和Phantomjs也很给力,Phantomjs虽然是无头浏览器,但有时定位不准),然后用Python写单元测试
request处理get/post请求写一堆代码自动化处理,都在后台运行,不用运行浏览器,非常适合处理表单
没有sikuli功能多,但是Python让生活更简单。
同时推荐一本Python网络数据采集(图灵社区取的名字_)的基础书籍Ryan Mitchell的《Web Scraping with Python》,可以和PyAutoGUI结合使用。
tl;dr
2015-08-17:输入中文bug没有解决,目前的解决方案是Python 2.X环境下安装pyperclip和pyautogui,用复制粘贴来实现。
In [ ]:
import pyperclip
import pyautogui
PyAutoGUI中文输入需要用粘贴实现
Python 2版本的pyperclip提供中文复制
def paste(foo):
pyperclip.copy(foo) pyautogui.hotkey('ctrl', 'v') 123
foo = u’学而时习之’
移动到文本框
pyautogui.click(130,30)
paste(foo)
1.简介
1.1 目的
PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。可以用pip安装,Github上有源代码。
下面的代码让鼠标移到屏幕中央。
In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)
PyAutoGUI可以模拟鼠标的移动、点击、拖拽,键盘按键输入、按住操作,以及鼠标+键盘的热键同时按住等操作,可以说手能动的都可以。
1.2 例子
In [ ]:
import pyautogui
screenWidth, screenHeight = pyautogui.size()
currentMouseX, currentMouseY = pyautogui.position()
pyautogui.moveTo(100, 150)
pyautogui.click()
鼠标向下移动10像素
pyautogui.moveRel(None, 10)
pyautogui.doubleClick()
用缓动/渐变函数让鼠标2秒后移动到(500,500)位置
use tweening/easing function to move mouse over 2 seconds.
pyautogui.moveTo(1800, 500, duration=2, tween=pyautogui.easeInOutQuad)
在每次输入之间暂停0.25秒
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’)
In [ ]:
distance = 200
while distance > 0:
pyautogui.dragRel(distance, 0, duration=0.5) # 向右 distance -= 5 pyautogui.dragRel(0, distance, duration=0.5) # 向下 pyautogui.draIn gRel(-distance, 0, duration=0.5) # 向左 distance -= 5 pyautogui.dragRel(0, -distance, duration=0.5) # 向上 1234567891011
1.4 保护措施(Fail-Safes)
就像《魔法师的学徒》(Sorcerer’s Apprentice)会担水的扫帚,可以担水,却无力阻止水漫浴室。你的程序也可能会失控(即使是按照你的意思执行的),那时就需要中断。如果鼠标还在自动操作,就很难在程序窗口关闭它。
为了能够及时中断,PyAutoGUI提供了一个保护措施。当pyautogui.FAILSAFE = True时,如果把鼠标光标在屏幕左上角,PyAutoGUI函数就会产生pyautogui.FailSafeException异常。如果失控了,需要中断PyAutoGUI函数,就把鼠标光标在屏幕左上角。要禁用这个特性,就把FAILSAFE设置成False:
In [ ]:
import pyautogui
pyautogui.FAILSAFE = False
通过把pyautogui.PAUSE设置成float或int时间(秒),可以为所有的PyAutoGUI函数增加延迟。默认延迟时间是0.1秒。在函数循环执行的时候,这样做可以让PyAutoGUI运行的慢一点,非常有用。例如:
In [ ]:
import pyautogui
pyautogui.PAUSE = 2.5
pyautogui.moveTo(100,100); pyautogui.click()
所有的PyAutoGUI函数在延迟完成前都处于阻塞状态(block)。(未来计划增加一个可选的非阻塞模式来调用函数。)
建议PAUSE和FAILSAFE一起使用。
2 安装与依赖
PyAutoGUI支持Python 2.x和Python 3.x
Windows:PyAutoGUI没有任何依赖,因为它用Python的ctypes模块所以不需要pywin32
pip3 install pyautogui
OS X:PyAutoGUI需要PyObjC运行AppKit和Quartz模块。这个模块在PyPI上的按住顺序是pyobjc-core和pyobjc
sudo pip3 install pyobjc-core
sudo pip3 install pyobjc
sudo pip3 install pyautogui
Linux:PyAutoGUI需要python-xlib(Python 2)、python3-Xlib(Python 3)
sudo pip3 install python3-xlib
sudo apt-get scrot
sudo apt-get install python-tk
sudo apt-get install python3-dev
sudo pip3 install pyautogui
3.速查表(小抄,Cheat Sheet)
3.1 常用函数
In [ ]:
import pyautogui
当前鼠标的坐标
pyautogui.position()
Out[ ]:
(123, 372)
In [ ]:
当前屏幕的分辨率(宽度和高度)
pyautogui.size()
Out[ ]:
(1920, 1080)
In [ ]:
(x,y)是否在屏幕上
x, y = 122, 244
pyautogui.onScreen(x, y)
Out[ ]:
True</
网址:pyautogui 鼠标键盘自动化 库的中文版 https://www.yuejiaxmz.com/news/view/187558
相关内容
Python RPA 流程自动化快速上手十个比较热门的免费开源桌面自动化应用/框架
【鼠标键盘回收 服务/价格信息】
废旧键盘鼠标...工艺品...废物利用...diy
电脑外设维护技巧正确清洁和保养鼠标键盘等
解决日常问题的 10 个 Python 自动化脚本
fences3中文破解版下载 fences win 桌面图标自动整理软件 v3.09.11 汉化绿色特别版 下载
鼠标一动,桌面即净:AutoHideDesktopIcons的神奇之处
Python自动化办公学习路线:提升工作效率,释放副业可能,让生活更美好!
记事本!