Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lissettecarlr committed Dec 22, 2021
1 parent 869b382 commit 6a4db9f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 66 deletions.
38 changes: 13 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
类似按键精灵,可以根据脚本实现对鼠标键盘的自动化操作。脚本可以通过录制也可以自己编写。
源码通过python编写,使用模块pyautogui来操作键盘鼠标和识别图片,使用pyWinhook模块来监听鼠标和键盘实现快捷键和录制的功能,使用PYQT5来实现界面。

**目前EXE包图片识别暂时无法用,python运行没问题,具体原因正在找**

## 环境
如果需要通过源码执行或者想自定义修改则需要安装以下的包,python使用的是3.9.6

* pyautogui
* pyautogui [文档](https://pyautogui.readthedocs.io/en/latest/screenshot.html)
```
pip install pyautogui
```
Expand All @@ -22,6 +23,12 @@ pip install pyqt5
pip install loguru
```

* opencv
图片匹配度参数需要,不安装也不会报错,只是无法使用图片匹配功能
```
pip install opencv-python
```

* pyWinhook
用于监听鼠标键盘的输入,直接安装会报错,需要手动去下载。
下列命令是查看设备支持的版本
Expand Down Expand Up @@ -143,37 +150,18 @@ pip debug --verbose
```

### 打包
关于用 pyinstaller的打包,之前直接执行命令发现编译出来的百多M,后面使用虚拟环境来打包要小一半。
* 1安装工具
```
pip install pipenv
```
* 2建立环境
```
pipenv install
```
* 3进入环境
```
pipenv shell
```
* 4安装相关模块
```
pip install pyautogui
pip install pyqt5
pip install loguru
pip install .\pk\pyWinhook-1.6.2-cp39-cp39-win_amd64.whl
pip install pyinstaller
```
* 5打包

抛弃之前用虚拟环境打包,直接
```
pyinstaller -Fw .\autogui.py
pyinstaller -Fw .\autogui.py --paths="C:\Users\dell\AppData\Local\Programs\Python\Python39\Lib\site-packages\cv2"
```

## 版本说明

### V1.4
增加了图片识别判断语句
增加了当脚本停止执行时自动恢复窗体
修复打包的EXE无法识别图片的BUG

### V1.3
增加空格键的录制
Expand Down Expand Up @@ -208,7 +196,7 @@ python .\autogui.py
```
如果需要生成exe可执行
```
pyinstaller -F autogui.py --noconsole
pyinstaller -F autogui.py --noconsole
```
界面如下
![](./pic/p_2.png)
Expand Down
34 changes: 34 additions & 0 deletions doc/pyautogui.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,38 @@ pyautogui.locateCenterOnScreen('help.png')
* 获取屏幕上所以符合的位置
```
list(pyautogui.locateAllOnScreen('help.png'))
```

* 获取图片坐标
```
picPos=pyautogui.locateCenterOnScreen(msg,confidence=0.7)
```
当使用confidence参数的时候需要安装opencv,不引入python执行时就可以用了

```
pip install opencv-python
```

引入
```
import cv2
```
如果引入报错
```
ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.
[21160] Failed to execute script 'autogui' due to unhandled exception!
```
卸载
```
pip uninstall opencv-python
pip uninstall opencv-contrib-python
```
去python\Lib\site-packages里面找vc2文件夹删掉
再安装
```
pip install opencv-python
```
打包的时候使用
```
pyinstaller .\autogui.py --paths="C:\Users\dell\AppData\Local\Programs\Python\Python39\Lib\site-packages\cv2"
```
103 changes: 62 additions & 41 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 该文件用于脚本的执行和停止

from os import terminal_size
import pyautogui
import time
import threading
import json
from loguru import logger
import configparser
import cv2

#由于录制按键值和操作接口的参数存在差异,所以需要个对照表
keyDict={
Expand Down Expand Up @@ -63,7 +63,6 @@ def run(self):
logger.info("scripts over")
else:
time.sleep(1)
pass
if(self.closeFlag == False):
break
logger.info('end')
Expand Down Expand Up @@ -171,9 +170,9 @@ def runScripts(self):
if(self.closeFlag == False or self.flag == False): #如果中途退出,则不在执行脚本了
logger.info("scripts exit")
return True
try:
if(taskType == "mouse"):
x,y = msg
if(taskType == "mouse"):
x,y = msg
try:
if(sta == "left down"):
#pyautogui.click(x,y,1,1,'left') #X,Y,点击次数,次数间隔,方式
pyautogui.mouseDown(x, y, 'left')
Expand All @@ -188,51 +187,73 @@ def runScripts(self):
else:
logger.error("mouse sta error!")
self.status = "当前语句执行错误,状态未知"
except:
self.suspend()
self.status = "[E]鼠标语句执行错误,已停止"
logger.error("mouse script error")
return True

elif(taskType == "keyboard"):
elif(taskType == "keyboard"):
try:
if(sta == "down"):
#pyautogui.keyDown(msg)
pyautogui.keyDown(keyDict[msg])
elif(sta == "up"):
pyautogui.keyUp(keyDict[msg])
elif(sta == "txt"):
pyautogui.typewrite(msg)
elif(taskType == "pic"):
#logger.debug(msg)
except:
self.suspend()
self.status = "[E]按键语句执行错误,已停止"
logger.error("keyboard script error")
return True

elif(taskType == "pic"):
#logger.debug(msg)

try:
picPos=pyautogui.locateCenterOnScreen(msg,confidence=self.pifConfidence)
if(picPos == None):
logger.error("not find pic : " + msg)
continue
if(sta == "left click"):
pyautogui.click(picPos.x,picPos.y,button='left')
elif(sta == "right click"):
pyautogui.click(picPos.x,picPos.y,button='night')
elif(sta == "left D click"):
pyautogui.doubleClick(picPos.x,picPos.y)
else:
logger.error("script error:sta")
self.status = "当前语句执行错误,状态未知"
# 判断图标是否存在的条件语句
elif(taskType == "ifpic"):
except:
self.suspend()
self.status = "[E]寻图语句执行错误,已停止"
logger.error("pic script error")
return True

if(picPos == None):
logger.error("not find pic : " + msg)
continue
if(sta == "left click"):
pyautogui.click(picPos.x,picPos.y,button='left')
elif(sta == "right click"):
pyautogui.click(picPos.x,picPos.y,button='night')
elif(sta == "left D click"):
pyautogui.doubleClick(picPos.x,picPos.y)
else:
logger.error("script error:sta")
self.status = "当前语句执行错误,状态未知"
# 判断图标是否存在的条件语句
elif(taskType == "ifpic"):
try:
picPos=pyautogui.locateCenterOnScreen(msg,confidence=self.pifConfidence)
if(picPos == None):
logger.error("not find pic : " + msg)
if(sta == "True" and picPos != None):
logger.info("ifpic is OK1")
isContinue = False
elif(sta == "False" and picPos == None):
logger.info("ifpic is OK2")
isContinue = False
else:
logger.info("ifpic is NOT")
isContinue = True
except:
self.suspend()
self.status = "[E]判断图语句执行错误,已停止"
logger.error("ifpic script error")
return True
if(picPos == None):
logger.error("not find pic : " + msg)
if(sta == "True" and picPos != None):
logger.info("ifpic is OK1")
isContinue = False
elif(sta == "False" and picPos == None):
logger.info("ifpic is OK2")
isContinue = False
else:
logger.error("script error:taskType")
self.status = "当前语句执行错误,类型未知"

except:
self.suspend()
self.status = "[E]当前语句执行错误,已停止"
logger.error("script error")

logger.info("ifpic is NOT")
isContinue = True
else:
logger.error("script error:taskType")
self.status = "当前语句执行错误,类型未知"



0 comments on commit 6a4db9f

Please sign in to comment.