This event class contains information about window and session close events.
The handler function for EVT_CLOSE
is called when the user has tried to close a a frame or dialog box using the window manager (X) or system menu (Windows). It can also be invoked by the application itself programmatically, for example by calling the wx.Window.Close
function.
You should check whether the application is forcing the deletion of the window using wx.CloseEvent.CanVeto
. If this is False
, you must destroy the window using wx.Window.Destroy
.
If the return value is True
, it is up to you whether you respond by destroying the window.
If you don’t destroy the window, you should call wx.CloseEvent.Veto
to let the calling code know that you did not destroy the window. This allows the wx.Window.Close
function to return True
or False
depending on whether the close instruction was honoured or not.
Example of a wx.CloseEvent handler:
def OnClose(self, event):
if event.CanVeto() and self.fileNotSaved:
if wx.MessageBox("The file has not been saved... continue closing?",
"Please confirm",
wx.ICON_QUESTION | wx.YES_NO) != wx.YES:
event.Veto()
return
self.Destroy() # you may also do: event.Skip()
# since the default event handler does call Destroy(), too
The EVT_END_SESSION
event is slightly different as it is sent by the system when the user session is ending (e.g. because of log out or shutdown) and so all windows are being forcefully closed. At least under MSW, after the handler for this event is executed the program is simply killed by the system. Because of this, the default handler for this event provided by wxWidgets calls all the usual cleanup code (including wx.App.OnExit
) so that it could still be executed and exit()s the process itself, without waiting for being killed. If this behaviour is for some reason undesirable, make sure that you define a handler for this event in your App-derived class and do not call event.Skip()
in it (but be aware that the system will still kill your application).
Handlers bound for the following event types will receive a wx.CloseEvent parameter.
wxEVT_CLOSE_WINDOW
command event, supplying the member function. This event applies to wx.Frame and wx.Dialog classes.wxEVT_QUERY_END_SESSION
session event, supplying the member function. This event can be handled in App-derived class only.wxEVT_END_SESSION
session event, supplying the member function. This event can be handled in App-derived class only.See also
__init__ |
Constructor. |
CanVeto |
Returns True if you can veto a system shutdown or a window close event. |
GetLoggingOff |
Returns True if the user is just logging off or False if the system is shutting down. |
GetVeto |
Returns whether the Veto flag was set. |
SetCanVeto |
Sets the ‘can veto’ flag. |
SetLoggingOff |
Sets the ‘logging off’ flag. |
Veto |
Call this from your event handler to veto a system shutdown or to signal to the calling application that a window close did not happen. |
wx.
CloseEvent
(Event)¶Possible constructors:
CloseEvent(commandEventType=wxEVT_NULL, id=0)
This event class contains information about window and session close events.
__init__
(self, commandEventType=wxEVT_NULL, id=0)¶Constructor.
Parameters: |
|
---|
CanVeto
(self)¶Returns True
if you can veto a system shutdown or a window close event.
Vetoing a window close event is not possible if the calling code wishes to force the application to exit, and so this function must be called to check this.
Return type: | bool |
---|
GetLoggingOff
(self)¶Returns True
if the user is just logging off or False
if the system is shutting down.
This method can only be called for end session and query end session events, it doesn’t make sense for close window event.
Return type: | bool |
---|
GetVeto
(self)¶Returns whether the Veto flag was set.
Return type: | bool |
---|
SetCanVeto
(self, canVeto)¶Sets the ‘can veto’ flag.
Parameters: | canVeto (bool) – |
---|
SetLoggingOff
(self, loggingOff)¶Sets the ‘logging off’ flag.
Parameters: | loggingOff (bool) – |
---|
Veto
(self, veto=True)¶Call this from your event handler to veto a system shutdown or to signal to the calling application that a window close did not happen.
You can only veto a shutdown if CanVeto
returns True
.
Parameters: | veto (bool) – |
---|
LoggingOff
¶See GetLoggingOff
and SetLoggingOff