Resource Not Found Exception in pyglet

 ̄綄美尐妖づ 提交于 2019-12-21 09:22:06

问题


I'm using Python 2.6.6 and pyglet 1.1.4. In my "Erosion" folder, I have "Erosion.py" and a folder named "Images." Inside images, there are .png images. One image is named "Guard.png."

In "Erosion.py" there is a segment that goes like so:

pyglet.resource.path = ['Images']
pyglet.resource.reindex()
self.image = pyglet.resource.image('%s%s' % (character, '.png'))

When I run this, I am given

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.

I have tried changing the path to ['./Images'] and ['../Images']. I've also tried removing the path and the reindex call and putting Erosion.py and Guard.png in the same folder.


回答1:


This is what i do to be able to load resources:

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)



回答2:


If the relative path is not working you can try with the absolute path using the os module.

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'))

It's better to use the os.path.join method instead of writing it as a string for a better cross-platform support.




回答3:


try this

pyglet.image.load('path/to/image.png')



回答4:


I get a problem like this using pyglet and pyscripter.(the text editor) In order for the file to be found I have to restart the editor before running the program.

This might be a problem with pyscripter however.




回答5:


It is worth looking into: http://pyglet.readthedocs.io/en/pyglet-1.3-maintenance/programming_guide/resources.html

You should include all the directories into:

 pyglet.resource.path = [...]

Then pass the ONLY the file name when you call:

pyglet.resource.image("ball_color.jpeg")

Try:

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()

The use of try and finally are not necessary, but they are recommended. You might end up with a lot of open windows and pyglet back processes if you don't close the window when there are errors.




回答6:


pyglet 1.4.4 format: HUD_img = pyglet.resource.image('ui\ui_gray_block_filled.png')

pyglet 1.4.8 format: HUD_img = pyglet.resource.image('ui/ui_gray_block_filled.png')

I was using pyglet 1.4.4 and was able to use this the first format. After upgrading to pyglet 1.4.8 I had the error: pyglet.resource.ResourceNotFoundException: Resource "your_resource_path" was not found on the path.

Switching to the 2nd format with forward slashes solved this issue for me. Not sure why this change was made...I'm sure there was a good reason for it.



来源:https://stackoverflow.com/questions/6541494/resource-not-found-exception-in-pyglet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!