python - 为什么我的 pyautogui 代码不能正常工作
问题描述
我创建了一个简单的脚本来在桌面上查找图像,然后输入“我找到了”,如果我隐藏图像,则脚本类型“我无法找到它”。
添加另一个动作时的问题,即我希望鼠标移动到图像的位置。该脚本运行良好,但是当我隐藏图像时,鼠标仍然移动到该位置,并且仍然键入我找到了它。但通常代码应该告诉我我找不到它。脚本仍在处理 if 而不是移至 else。
我的代码是:
import pyautogui
import time
location = pyautogui.locateOnScreen('image.png', confidence = 0.6)
while 1:
if location:
print("I found it ")
time.sleep(2)
print(pyautogui.moveTo(location))
else:
print("I am unable to found it")
解决方案
你存储pyautogui.locateOnScreen('image.png', confidence = 0.6)
在变量中location
。然后检查条件(if
/ else
)。但是,您永远不会重新检查pyautogui.locateOnScreen('image.png', confidence = 0.6)
. 我仍然不确定您要在这里实现什么,但至少检查需要进入while
:
while 1:
location = pyautogui.locateOnScreen('image.png', confidence = 0.6)
if location:
print("I found it ")
time.sleep(2)
print(pyautogui.moveTo(location))
else:
print("I am unable to find it")
推荐阅读
- html - 如何在php中设置三个水平图像
- php - 检索两个独立列的总和 - 使用嵌套选择语句
- django - price() 在尝试计算 Django Rest Framework 中的价格时缺少 1 个必需的位置参数:“self”
- node.js - 如何将参数从角度 ts 前端发送到 nodejs 后端
- ruby - 使用可重用代码创建 Ruby 构建器对象
- core-data - 如何避免更改 NSBatchInsertRequest 中的属性值?
- tensorflow - Keras 的 Scipy 优化器界面
- android - 单个 Spinner 项目的自定义格式不起作用
- c# - Blazor WASM - 如何通过 http 启用请求
- javascript - 以编程方式更改 html 选择元素不会触发事件