我使用pyglets Sprite.delete()错误吗?

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

我想删除一个pyglet精灵。我正在尝试在sprite对象上使用delete()方法。遵循测试脚本...

import pyglet

window = pyglet.window.Window()

x = pyglet.sprite.Sprite(img=pyglet.image.load('resource/icon.png'))
draw_objects = [x]


@window.event
def on_draw():
    window.clear()
    for obj in draw_objects:
        obj.draw()


@window.event
def on_key_press(symbol, modifiers):
    if symbol == pyglet.window.key.DELETE:
        print('deleting')
        global x
        x.delete()


pyglet.app.run()

...产生此错误:

Traceback (most recent call last):
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\test.py", line 24, in <module>
    pyglet.app.run()
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\venv\lib\site-packages\pyglet\app\__init__.py", line 107, in run
    event_loop.run()
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\venv\lib\site-packages\pyglet\app\base.py", line 167, in run
    timeout = self.idle()
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\venv\lib\site-packages\pyglet\app\base.py", line 243, in idle
    window.dispatch_event('on_draw')
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\venv\lib\site-packages\pyglet\window\__init__.py", line 1333, in dispatch_event
    if EventDispatcher.dispatch_event(self, *args) != False:
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\venv\lib\site-packages\pyglet\event.py", line 408, in dispatch_event
    if handler(*args):
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\test.py", line 13, in on_draw
    obj.draw()
  File "C:\Users\Finn\Data\PythonProjects\_tests\firstPyglet\venv\lib\site-packages\pyglet\sprite.py", line 672, in draw
    self._group.set_state_recursive()
AttributeError: 'NoneType' object has no attribute 'set_state_recursive'

我的代码是否有问题,或者pyglet有问题吗?

python sprite pyglet
1个回答
0
投票

draw_objects.remove(x)之后添加x.delete()。该错误是由于您试图在以前已删除但尚未从绘图列表中删除其变量的适当函数中绘制精灵而造成的。

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