在pyglet中未找到资源异常

在pyglet中未找到资源异常,pyglet,Pyglet,我正在使用Python 2.6.6和pyglet 1.1.4。在我的“侵蚀”文件夹中,我有“侵蚀.py”和一个名为“Images”的文件夹。在图像中,有.png图像。其中一个图像名为“Guard.png” 在“侵蚀.py”中有一段是这样的: pyglet.resource.path = ['Images'] pyglet.resource.reindex() self.image = pyglet.resource.image('%s%s' % (character, '.png')) 当我运

我正在使用Python 2.6.6和pyglet 1.1.4。在我的“侵蚀”文件夹中,我有“侵蚀.py”和一个名为“Images”的文件夹。在图像中,有.png图像。其中一个图像名为“Guard.png”

在“侵蚀.py”中有一段是这样的:

pyglet.resource.path = ['Images']
pyglet.resource.reindex()
self.image = pyglet.resource.image('%s%s' % (character, '.png'))
当我运行这个时,我得到了

File "C:\Python26\lib\site-packages\pyglet\resource.py", line 394, in file raise ResourceNotFoundException(name)
ResourceNotFoundException: Resource "Guard.png" was not found on the path.  Ensure that the filename has the correct captialisation.

我已尝试将路径更改为['./Images']和['../Images']。我还尝试删除路径和reindex调用,并将persolution.py和Guard.png放在同一个文件夹中。

试试看


我在使用pyglet和pyscripter(文本编辑器)时遇到了这样的问题。为了找到文件,我必须在运行程序之前重新启动编辑器


这可能是pyscripter的一个问题。

这是我为能够加载资源所做的:

pyglet.resource.path = ['C:\\Users\\myname\\Downloads\\temp']
pyglet.resource.reindex()

pic = pyglet.resource.image('1.jpg')

texture = pic.get_texture()
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST)

texture.width = 960
texture.height = 720

texture.blit(0, 0, 0)

如果相对路径不起作用,可以使用操作系统模块尝试使用绝对路径

import pyglet
import os

working_dir = os.path.dirname(os.path.realpath(__file__))
pyglet.resource.path = [os.path.join(working_dir,'Images')]
pyglet.resource.reindex()

image = pyglet.resource.image('character.png'))

最好使用os.path.join方法,而不是将其作为字符串写入,以获得更好的跨平台支持。

值得研究:

您应该将所有目录包括到:

 pyglet.resource.path = [...]
然后在调用时仅传递文件名:

pyglet.resource.image("ball_color.jpeg")
尝试:


不需要使用
try
finally
,但建议使用。如果出现错误时不关闭窗口,可能会导致许多打开的窗口和pyglet返回进程。

pyglet 1.4.4格式:HUD\u img=pyglet.resource.image('ui\ui\u gray\u block\u filled.png'))

pyglet 1.4.8格式:HUD\u img=pyglet.resource.image('ui/ui\u gray\u block\u filled.png')

我使用的是Pyglet1.4.4,并且能够在第一种格式中使用它。 升级到pyglet 1.4.8后,出现错误:pyglet.resource.ResourceNotFoundException:resource在路径上找不到“您的资源路径”


用前斜杠切换到第二种格式解决了这个问题。不知道为什么要做此更改…我确信这是有充分理由的。

有关加载资源的详细信息如果您确实需要资源处理程序,请参阅。我之所以使用它,是因为它给了我更多的额外参数,可以通过GL库轻松快速地使用。这太糟糕了。当我调用“reindex()”时,我的计算机似乎对所有内容都进行了索引,而且需要几分钟。@BerryTsakala它的设计目的是为您提供的补丁下面的文件夹中的所有内容编制索引。所以不要把你的脚本放在`\Downloads`中并从那里建立索引,而是为你的项目使用一个特定的文件夹;在哪里不重要。无论如何,我在windows中使用它时遇到了很多麻烦(例如,使用avbin),因此我完全放弃了pyglet,转而使用其他模块。如果pyglet不允许直接指定完整路径名,这在pyglet中是一个问题。您可能必须重新启动资源文件列表。您是否假设'character.png'正在使用_dir+“/Images/”
pyglet.resource.image("ball_color.jpeg")
import os
import pyglet

working_dir = os.path.dirname(os.path.realpath(__file__))
image_dir = os.path.join(working_dir, 'images')
sound_dir = os.path.join(working_dir, 'sound')

print working_dir
print "Images are in: " + image_dir
print "Sounds are in: " + sound_dir

pyglet.resource.path = [working_dir, image_dir, sound_dir]
pyglet.resource.reindex()

try:
    window = pyglet.window.Window()
    image = pyglet.resource.image("ball_color.jpeg")

    @window.event
    def on_draw():
        window.clear()
        image.blit(0, 0)

    pyglet.app.run()

finally:
    window.close()