Search code examples
pythonpyglet

Closing-Event of window in Pyglet


After closing Pyglet by the "X" of the window, I assume the Event "on_close" would be triggered, but it doesn't seems so. What do I do wrong?

window = pyglet.window.Window(fullscreen=False)

@window.event
def on_draw():
    sprite.draw()
    label.draw()

def on_close():
    print("I'm closing now") #<- this never happens

Complete Script here (Line 175): https://github.com/VirToReal/py-slideshow/blob/master/slideshow.py


Solution

  • import pyglet
    
    window = pyglet.window.Window(fullscreen=False)
    
    @window.event
    def on_draw():
        window.clear()
        window.flip()
    
    @window.event
    def on_close():
        print("I'm closing now") #<- this never happens
    
    pyglet.app.run()
    

    You've got two problems, one - you're missing pyglet.app.run() or any form of event dispatching in your application.

    The other problem is that you didn't use a decorator for the on_close() function.
    The previously declared decorator is only for that function, that is on_draw().

    Some other side notes:
    You didn't clear the window and some times flip() needs to be called, might be good to remember.