简体   繁体   中英

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

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?

The redraw event is on_draw() rather than 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

[...] 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. [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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