PyAutoGUI 代码在 Visual Studio Code 中工作正常,但否则我会收到“无此类文件”错误

问题描述 投票:0回答:1

我的代码中有这一行,并且在同一文件位置有一张图片

buttonlocation = pyautogui.locateCenterOnScreen('button.png')

此代码在 Visual Studio Code 中运行良好,但每当我尝试运行 .py 文件本身时,我都会收到此错误

FileNotFoundError: [Errno 2] No such file or directory: 'button.png'

我必须指定完整路径还是我遗漏了什么?

python pyautogui
1个回答
0
投票

我刚刚解决了类似的问题。

最有可能的是,您的

'button.png'
位于 VS Code 内设置为工作目录的文件夹中。因此,代码运行并且您得到正确的输出。

在其他情况下,您没有设置与工作目录相同的文件夹,这会产生影响。

诀窍是将文件

'button.png'
保留在正确的文件夹中。

© www.soinside.com 2019 - 2024. All rights reserved.