简体   繁体   English

在 pyglet 中不使用装饰器会导致问题吗?

[英]Does not using a decorator in pyglet cause problems?

I was making a simple code in pyglet to draw a rectangle on the screen but when i used the proper code format, nothing happens but no errors are thrown up either我在 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()

this code just results in a black screen.此代码只会导致黑屏。 The rectangle is not printed but if i use this code instead矩形未打印,但如果我使用此代码代替

window=pyglet.window.Window()
rect=pyglet.shapes.Shapes(0,0,50,50,color=(255,255,255))
    
def draw_():
    rect.draw()
pyglet.app.run()

the rectangle is printed.矩形被打印出来。 As the 2nd code is not the standard method of drawing a shape in pyglet, i wanted to know if there is anything wrong with doing that(performance issues, glitches etc) If the 2nd code is wrong, what should i do instead?由于第二个代码不是在 pyglet 中绘制形状的标准方法,我想知道这样做是否有任何问题(性能问题、故障等)如果第二个代码错误,我应该怎么做?

The redraw event is on_draw() rather than draw_ :重绘事件是on_draw()而不是draw_

@window.event
def on_draw():
    rect.draw()

If you don't use a decorator, the default event handler is completely replaced:如果您不使用装饰器,则默认事件处理程序将被完全替换:

 def on_draw(): rect.draw()

When you use the decoration, an additional event handler is added.当您使用装饰时,会添加一个额外的事件处理程序。 Therefore the default handler is kept:因此保留默认处理程序:

 @window.event def on_draw(): rect.draw()

See PyGlet - Setting event handlers请参阅PyGlet - 设置事件处理程序

[...] The simplest way is to directly attach the event handler to the corresponding attribute on the object. [...] 最简单的方法是将事件处理程序直接附加到 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. [...] 如果您不想替换默认事件处理程序,而是想添加一个额外的事件处理程序,pyglet 提供了使用事件装饰器的快捷方式。 Your custom event handler will run, followed by the default event handler.您的自定义事件处理程序将运行,然后是默认事件处理程序。 [...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM