无法在Atom中使用PyGame打开图像 [英] Cannot Open Images With PyGame in Atom

查看:110
本文介绍了无法在Atom中使用PyGame打开图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此网站

我正在尝试像这样加载图像:

I am trying to load an image like so:

background_image = pygame.image.load("background.jpg").convert()

我发现有其他几个帖子问同样的问题,所有答案都告诉发布者确保图像与.py文件位于同一文件夹中.好吧,是的.我仔细检查了.

I have found several other posts asking the same question, and all the answers are telling the poster to make sure the image is in the same folder as the .py file. Well, it is. I double checked.

我还尝试将其放入名为'images'的子文件夹中并使用它.

I also tried putting it into a subfolder called 'images' and using this.

background_image = pygame.image.load(os.path.join("images","background.jpg")).convert()

在两种情况下,我都会收到以下错误消息.

In both cases I get the following error message.

pygame.error: Couldn't open saturn_family1.jpg

pygame.error: Couldn't open images\saturn_family1.jpg

使用绝对路径时,它可以工作.但是我不应该能够使用这样的相对路径吗?

When using the absolute path, it works. But shouldn't I be able to use a relative path like this?

此外,当使用script包在Atom中执行时,也会发生这种情况.但是,当执行实际的python文件时,它可以工作.

Also, this happens when using the script package to execute in Atom. But when executing the actual python file, it works.

我很沮丧,这似乎没有任何意义!发生了什么事?

I am very frustrated, this doesn't seem to make any sense! What's going on?

完整的错误消息是:

C:\Users\Manu\Dropbox\Python\ProgramArcadeGames
Traceback (most recent call last):
  File "C:\Users\Manu\Dropbox\Python\ProgramArcadeGames\Ch11\11_graphicsAndSound.py", line 26, in <module>
    background_image = pygame.image.load(os.path.join("images","saturn_family1.jpg")).convert()
pygame.error: Couldn't open images\saturn_family1.jpg
[Finished in 0.815s]

这是.py目录:

更新: 我在sublime中运行了完全相同的代码,并且效果很好.我假设这是Atom的问题,所以从现在开始我将只使用Sublime.感谢所有尝试提供帮助的人!

UPDATE: I ran the exact same code in sublime and it worked fine. I am assuming this is a problem with Atom, so I will just use Sublime from now on. Thanks to everyone who tried to help!

推荐答案

将文件放在与python程序相同的目录中将是可行的,前提是这是当前的工作目录.由于我们知道情况并非如此,因此要么在执行脚本时以某种方式更改工作目录,要么在其他目录中启动.您可以使用

Putting the file in the same directory as your python program will work provided that is the current working directory. Since we know this is not the case, either you are somehow changing the working directory while the script is executing, or you are starting in some other directory. You could do this on the command line with

python ../myscript.py

但我不认为您正在这样做.您说了一些有关在Atom下运行脚本的信息,这也许可以解释它.我对此没有任何经验,所以我不能随便说.请输入

but I don't suppose you're doing this. You say something about running the script under Atom, which may explain it. I don't have any experience with this, so I can't say offhand. Please put

print os.getcwd()

作为程序的第一行.然后,您将知道从哪里开始以及当前目录是否正在更改.如果没有更改,请尝试在Atom下并从命令行运行脚本,以查看是否获得了不同的结果.除非我们确切地知道发生了什么,否则我们将无法解决问题.

as the very first line in your program. Then you'll know where you're starting and whether the current directory is changing. If it's not changing, try running the script under Atom and from the command line to see if you get different results. We won't be able to solve the problem until we know exactly what is happening.

这篇关于无法在Atom中使用PyGame打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆