我使用 pyglets Sprite.delete() 错了吗?
Am I using pyglets Sprite.delete() wrong?
我想删除一个 pyglet 精灵。我正在尝试在精灵对象上使用 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有问题?
在x.delete()
之后添加draw_objects.remove(x)
。该错误是由于您试图在先前已删除的适当函数中绘制精灵,但尚未从绘图列表中删除其变量。
@window.event
def on_key_press(symbol, modifiers):
global x
if symbol == pyglet.window.key.DELETE:
if x in draw_objects:
print('deleting')
x.delete()
draw_objects.remove(x)
我想删除一个 pyglet 精灵。我正在尝试在精灵对象上使用 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有问题?
在x.delete()
之后添加draw_objects.remove(x)
。该错误是由于您试图在先前已删除的适当函数中绘制精灵,但尚未从绘图列表中删除其变量。
@window.event
def on_key_press(symbol, modifiers):
global x
if symbol == pyglet.window.key.DELETE:
if x in draw_objects:
print('deleting')
x.delete()
draw_objects.remove(x)