首页 技术 正文
技术 2022年11月14日
0 收藏 704 点赞 3,085 浏览 1513 个字

pyautogui模块

PyAutoGUI——让所有GUI都自动化

安装代码:

pip install pyautogui

目的

PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,Linux)。可以用pip安装,Github上有源代码。

下面的代码让鼠标移到屏幕中央。

import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

PyAutoGUI可以模拟鼠标的移动、点击、拖拽,键盘按键输入、按住操作,以及鼠标+键盘的热键同时按住等操作,可以说手能动的都可以。

pyautogui基本操作样例

import pyautogui# 获取当前屏幕分辨率screenWidth, screenHeight = pyautogui.size()# 获取当前鼠标位置currentMouseX, currentMouseY = pyautogui.position()# 鼠标移动坐标为100,100位置  绝对移动pyautogui.moveTo(100, 100)# 鼠标左击pyautogui.click()# 鼠标乡下移动  相对移动pyautogui.moveRel(None, 10)# 鼠标双击pyautogui.doubleClick()# 用缓动/渐变函数让鼠标2秒后移动到(500,500)位置# use tweening/easing function to move mouse over 2 seconds.pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.easeInOutQuad)# 在每次输入之间暂停0.25秒pyautogui.typewrite('Hello world!', interval=0.25)# 键盘点击escpyautogui.press('esc')# 按住shift键pyautogui.keyDown('shift')
pyautogui.press(['left', 'left', 'left', 'left', 'left', 'left'])# 放开shift键pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')

PyAutoGUI键盘表:

‘enter’ (或‘return’ 或 ‘\n’) 回车
‘esc’ ESC键
‘shiftleft’, ‘shiftright’ 左右SHIFT键
‘altleft’, ‘altright’ 左右ALT键
‘ctrlleft’, ‘ctrlright’ 左右CTRL键
‘tab’ (‘\t’) TAB键
‘backspace’, ‘delete’ BACKSPACE 、DELETE键
‘pageup’, ‘pagedown’ PAGE UP 和 PAGE DOWN键
‘home’, ‘end’ HOME 和 END键
‘up’, ‘down’, ‘left’, ‘right’ 箭头键
‘f1’, ‘f2’, ‘f3’…. F1…….F12键
‘volumemute’, ‘volumedown’, ‘volumeup’ 有些键盘没有
‘pause’ PAUSE键
‘capslock’, ‘numlock’, ‘scrolllock’ CAPS LOCK, NUM LOCK, 和 SCROLL LOCK 键
‘insert’ INS或INSERT键
‘printscreen’ PRTSC 或 PRINT SCREEN键
‘winleft’, ‘winright’ Win键
‘command’ Mac OS X command键
下一篇: 编辑器之神_vim
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292