Search code examples
pythonpyglet

How to close a pyglet window?


I have a following problem. Suppose I have a following code:

@window.event
def on_key_press( symbol, mod):
    if condition == True:
       return print("Game over")

pyglet.app.run()

I need to also to close the window after the condition == True is met. How can I do that please? I tried:

@window.event
def on_key_press( symbol, mod):
    if condition == True:
       return print("Game over")
       pyglet.app.exit()

but it did not work. Thanks a lot for any help!


Solution

  • The function is terminated at the the first occurrence of return in the control flow. You have to remove the return statement.

    @window.event
    def on_key_press( symbol, mod):
        if condition == True:
    
           # return print("Game over") <--- DELETE
           
           print("Game over")
    
           pyglet.app.exit()