在 pyglet 中不使用装饰器会导致问题吗?
Does not using a decorator in pyglet cause problems?
我在 pyglet 中编写了一个简单的代码来在屏幕上绘制一个矩形,但是当我使用正确的代码格式时,没有任何反应,但也没有抛出任何错误
window=pyglet.window.Window()
rect=pyglet.shapes.Shapes(0,0,50,50,color=(255,255,255))
@window.event
def draw_():
rect.draw()
pyglet.app.run()
此代码只会导致黑屏。矩形未打印,但如果我改用此代码
window=pyglet.window.Window()
rect=pyglet.shapes.Shapes(0,0,50,50,color=(255,255,255))
def draw_():
rect.draw()
pyglet.app.run()
打印矩形。
由于第二个代码不是在 pyglet 中绘制形状的标准方法,我想知道这样做是否有任何问题(性能问题、故障等)
如果第二个代码错误,我应该怎么做?
重绘事件是on_draw()
而不是draw_
:
@window.event
def on_draw():
rect.draw()
如果你不使用装饰器,默认的事件处理程序被完全替换:
def on_draw():
rect.draw()
当您使用装饰时,会添加一个额外的事件处理程序。因此保留默认处理程序:
@window.event
def on_draw():
rect.draw()
见PyGlet - Setting event handlers
[...] The simplest way is to directly attach the event handler to the corresponding attribute on the object. This will completely replace the default event handler. [...]
[...] If you don’t want to replace the default event handler, but instead want to add an additional one, pyglet provides a shortcut using the event decorator. Your custom event handler will run, followed by the default event handler. [...]
我在 pyglet 中编写了一个简单的代码来在屏幕上绘制一个矩形,但是当我使用正确的代码格式时,没有任何反应,但也没有抛出任何错误
window=pyglet.window.Window()
rect=pyglet.shapes.Shapes(0,0,50,50,color=(255,255,255))
@window.event
def draw_():
rect.draw()
pyglet.app.run()
此代码只会导致黑屏。矩形未打印,但如果我改用此代码
window=pyglet.window.Window()
rect=pyglet.shapes.Shapes(0,0,50,50,color=(255,255,255))
def draw_():
rect.draw()
pyglet.app.run()
打印矩形。 由于第二个代码不是在 pyglet 中绘制形状的标准方法,我想知道这样做是否有任何问题(性能问题、故障等) 如果第二个代码错误,我应该怎么做?
重绘事件是on_draw()
而不是draw_
:
@window.event
def on_draw():
rect.draw()
如果你不使用装饰器,默认的事件处理程序被完全替换:
def on_draw(): rect.draw()
当您使用装饰时,会添加一个额外的事件处理程序。因此保留默认处理程序:
@window.event def on_draw(): rect.draw()
见PyGlet - Setting event handlers
[...] The simplest way is to directly attach the event handler to the corresponding attribute on the object. This will completely replace the default event handler. [...]
[...] If you don’t want to replace the default event handler, but instead want to add an additional one, pyglet provides a shortcut using the event decorator. Your custom event handler will run, followed by the default event handler. [...]