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
See also samples/dialogs
for a full example of interrupting closing an application when there are e.g. unsaved files.
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.
EVT_CLOSE: Process a wxEVT_CLOSE_WINDOW
command event, supplying the member function. This event applies to wx.Frame and wx.Dialog classes.
EVT_QUERY_END_SESSION: Process a wxEVT_QUERY_END_SESSION
session event, supplying the member function. This event can be handled in App-derived class only.
EVT_END_SESSION: Process a wxEVT_END_SESSION
session event, supplying the member function. This event can be handled in App-derived class only. ^^
Constructor. |
|
Returns |
|
Returns |
|
Returns whether the Veto flag was set. |
|
Sets the ‘can veto’ flag. |
|
Sets the ‘logging off’ flag. |
|
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. |
See |
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.
commandEventType (wx.EventType) –
id (int) –
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.
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.
bool
GetVeto
(self)¶Returns whether the Veto flag was set.
bool
SetCanVeto
(self, canVeto)¶Sets the ‘can veto’ flag.
canVeto (bool) –
SetLoggingOff
(self, loggingOff)¶Sets the ‘logging off’ flag.
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
.
veto (bool) –
LoggingOff
¶See GetLoggingOff
and SetLoggingOff