python之PyAutoGui教你做个自动脚本计算器的方法
作者:稳健小周 时间:2022-01-08 15:24:55
前提环境准备
python3+pillow+pyautogui
先提前安装好python3以及pillow和pyautogui模块
这里介绍一下模块安装方法
pip install pillow
pip install pyautogui
pip install opencv-python
最终效果是利用python脚本模拟电脑计算器进行自动计算,相当于模拟人去点击自带的计算器进行运算,想要做到这一点需要有两个条件:
1.模拟鼠标和键盘的输入工作
2.识别计算器按钮的位置
先来看一下win10电脑的计算器是什么样子的:
我们要知道一点,计算器窗口的位置每次都是不同的,如果你是固定去确定按钮的坐标那就太被动了,所以我们这里需要用到图像识别,去识别到按钮的位置,博主这里
做一个示例 做一个1+2=的运算。
废话不多说直接上代码,跟着注释,看懂代码没毛病。
打开你的微信截图截下1,+,2,=四个图片存入脚本所在目录
详细代码
#导入模块
from PIL import ImageGrab
import pyautogui as auto
#定义类
class Screenshoot:
def __init__(self):
#self.bbox = bbox
#self.name = name
#self.im = ImageGrab.grab(self.bbox)
#定位xy坐标,confidence为相似度判断,最好不要使用1.0完全相似,比较容易不识别
self.position_1 = auto.locateCenterOnScreen('1.png', confidence=0.9)
self.position_2 = auto.locateCenterOnScreen('2.png', confidence=0.9)
self.position_3 = auto.locateCenterOnScreen('+.png', confidence=0.9)
self.position_4 = auto.locateCenterOnScreen('=.png', confidence=0.9)
pass
def fullshoot(self):
#全屏截图
#self.im.save('01.png')
pass
def partialshoot(self):
#局部精确截图
#self.im.save(self.name+'.png')
pass
def position_show(self):
#打印各坐标
print(self.position_1)
print(self.position_2)
print(self.position_3)
print(self.position_4)
def caculate(self):
#依次点击按钮
auto.click(self.position_1)
auto.click(self.position_3)
auto.click(self.position_2)
auto.click(self.position_4)
#对象初始化
shoot1 = Screenshoot()
#对象函数执行
shoot1.position_show()
shoot1.caculate()
#shoot1.partialshoot()
#shoot1.fullshoot()
运行结果
来源:https://blog.csdn.net/weixin_45112130/article/details/114391092
标签:PyAutoGui,自动脚本计算器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python使用 Beanstalkd 做异步任务处理的方法
2021-01-06 18:18:19
![](https://img.aspxhome.com/file/2023/1/75151_0s.png)
MYSQL创建触发程序的方法
2009-07-30 08:38:00
php延迟静态绑定实例分析
2023-11-23 11:44:57
Python中三个不可思议的返回功能分享
2021-11-21 07:32:41
php比较两个指定的日期的实例讲解
2023-06-13 12:11:29
python获取文件版本信息、公司名和产品名的方法
2022-05-31 17:30:23
Python 字符串处理特殊空格\\xc2\\xa0\\t\\n Non-breaking space
2022-03-09 19:20:16
![](https://img.aspxhome.com/file/2023/2/107592_0s.png)
XHTML 1.0:标记新的开端
2023-08-04 21:59:30
python多线程扫描端口示例
2022-06-28 23:18:58
python使用装饰器和线程限制函数执行时间的方法
2022-04-03 04:06:34
Python 如何批量更新已安装的库
2023-06-05 12:15:40
![](https://img.aspxhome.com/file/2023/9/105269_0s.jpg)
Python 忽略文件名编码的方法
2021-06-11 15:16:55
Python使用MapReduce编程模型统计销量
2021-07-16 14:24:43
![](https://img.aspxhome.com/file/2023/8/102788_0s.png)
Python基础 while循环与break、continue关键字
2023-11-19 03:41:42
![](https://img.aspxhome.com/file/2023/3/64283_0s.jpg)
再论Javascript的类继承
2010-06-26 12:48:00
Python使用shutil模块实现文件拷贝
2021-02-01 14:46:28
Python asyncore socket客户端开发基本使用教程
2021-01-25 11:06:39
![](https://img.aspxhome.com/file/2023/6/90836_0s.png)
python-httpx的具体使用
2023-08-12 00:25:53
python pycurl验证basic和digest认证的方法
2022-12-17 23:01:15
Django 2.0版本的新特性抢先看!
2021-01-03 11:10:50