Problem with EVT_CLOSE called twice in a derived wx.Dialog

Hello,

I would like to catch the EVT_CLOSE event in a derived wx.Dialog class to be able to perform some actions prior the dialog is effectively closed, for example saving data to disk, etc.

For this I mapped the event using “self.Bind(wx.EVT_CLOSE, self.OnClose)”. The event gets called when I click either ‘OK’ or ‘Cancel’ buttons, but when I close the dialog using ALT+F4 or the little cross (X) on the upper-right corner, the event gets fired twice…

Now if I comment ‘dlg.Close()’ (line #67 in my sample app), the event is called only once when using ALT+F4 or (X), but do not gets called when ‘OK’ or ‘Cancel’ are pressed.

Any idea how to get the EVT_CLOSE event only fired once, whatever the method used to close the window (ALT+F4, (X), 'OK or ‘Cancel’) ?

Please find a sample attached.

I am on Windows XP, I tried both wxPython 2.8.12.1 and 2.9.4.0… it does the same

Best regards

Jerome

test (pb close event).py (1.98 KB)

I changed it a bit but still see the same problem as you, as per doc
the EVT_CLOSE should fire with the “X” and with buttons with id’s
wx.ID_CANCEL and wx.ID_OK, but …
A work around would be to call your clean up action based on the
return value you get from ShowModal - see attached.
My tests are with 2.9.5.0.b20121231 msw (classic)
Werner
P.S.
PySimpleApp is deprecated in 2.9, I use the WIT all the time as it
is a very useful debug tool -

diagclose.py (2.64 KB)

···

Hi Jerome,

  On 08/02/2013 10:18, jeromecoroyer wrote:

Hello,

    I would like to catch the EVT_CLOSE event in a derived

wx.Dialog class to be able to perform some actions prior the
dialog is effectively closed, for example saving data to disk,
etc.

      For this I mapped the event

using “self.Bind(wx.EVT_CLOSE, self.OnClose)”. The event gets
called when I click either ‘OK’ or ‘Cancel’ buttons, but when
I close the dialog using ALT+F4 or the little cross (X) on the
upper-right corner, the event gets fired twice…

Now if I comment ’ dlg.Close()’ (line #67 in my sample
app), the event is called only once when using ALT+F4 or (X),
but do not gets called when ‘OK’ or ‘Cancel’ are pressed.

Any idea how to get the EVT_CLOSE
event only fired once, whatever the method used to close the
window (ALT+F4, (X), 'OK or ‘Cancel’) ?

Please find a sample attached.

    I am on Windows XP, I tried both wxPython 2.8.12.1 and

2.9.4.0… it does the same

http://wiki.wxpython.org/Widget%20Inspection%20Tool

Hi Werner, thanks for your answer

I changed it a bit but still see the same problem as you, as per doc

the EVT_CLOSE should fire with the “X” and with buttons with id’s
wx.ID_CANCEL and wx.ID_OK, but …

Yes quite strange, maybe we miss something here but I don’t see it… Maybe Robin will have a clue on this issue, we will see.

A work around would be to call your clean up action based on the

return value you get from ShowModal - see attached.

I did the same kind of workaround and it is fine now.

P.S.

PySimpleApp is deprecated in 2.9, I use the WIT all the time as it

is a very useful debug tool -
http://wiki.wxpython.org/Widget%20Inspection%20Tool

Thanks for the tip, I will use this in my future sample apps…

Regards

Jerome