Get the onWindowClose event

Hi, i’m searching for a long time and i dont understand how to get the onWindowClose event.
Do someone have a simple example ?



Listen for ‘window-event’, and then check win.getProperties().getOpen() to see if the window is no longer open. There’s a fine example of this in direct/src/showbase/


Hi, and thanks for your fast and precise answer.
Ok, now i understand better, but i still can’t get the on close : properties.getOpen() always return 1

I have done all like in :

self.accept('window-event', self.onClose)

and the func :

  def onClose(self, win):
      if win == base.winList[0]:
          properties = win.getProperties()
          print "Got window event: %s" % (repr(properties))
          print "getOpen = "+str(properties.getOpen())
          if not properties.getOpen(): #this never appen
              print "User closed main window."

Maybe it’s because when i close the window, python script is ending before executing onClose()
But why ?

I must say that my english is very poor, so understanding the doc is quiet difficult …

thanks again


That’s because ShowBase is calling this code as well, and as you can see, it checks if the window is still open – if not, it calls self.userExit() to shut down the application.
One way to prevent this is by setting self.userExit to an empty function:

base.userExit = lambda: None

That will prevent your program from exiting when you close the window, but you do need to close the program yourself then when you finish with the window.

Ok, thanks, but i realise that my code was in a class, so instead of self.accept , i use base.accept, and it’s working quiet well exept that double exec returns error.
Now with

base.userExit = lambda: None

it works perfectly

Topic is solve

Many thanks