Close the wx.Frame with a StyledTextCtrl after confirming Save or not by a dialog box

A simple text editor with wx.Frame, StyledTextCtrl, and some basic menue items : As File menue; Open, Save, Save As, Close : as Edit menue; Copy, Cut, Paste, Undo, Redo.

Operations are just open file, do something, and close. Quite simple.

If the data in the StyledTextCtrl is changed, I want to open a dialog box (ShowModal()) to confirm save or not by both the Xbox of the wx.Frame and the Close(Cmd-W) menu.

To get whether the data is changed or not, when the stc.EVT_STC_CHANGE is sent to the application, the variable I set as ‘flagDirty’=True.(This editor doesn’t need to save the Style change, so stc.EVT_STC_CHANGE, not stc.EVT_STC_MODIFIED.)

I bind in the wx.Frame’s init(self, title) function, I bound the Close event to handler.

self.Bind(wx.EVT_CLOSE, self.OnClose)

And the Close menuitem

menu_File_Close = menu_File.Append(wx.ID_CLOSE, “&Close”+"\t"+“Ctrl+W”, “Close”)

Xbox seems work all right.

The menuitem Close is the problem. The dialog displayed OK. But when No(= wx.ID_NO );

THE PROBLEMS ARE::

1 At the first time pushed, the dialog doesn’t work. Second time, dialog closes, but the wx.Frame doesn’t close.

Why dialog works funny like this?

2 Isn’t there BETTER WAY for the case like this?

For example, I’m not sure about setting variable like ‘flagDirty’ is good for getting whether the data is changed.

3 When just open the window and close without editing, the dialog box opens for the variable is True. What’s wrong with the setting of the StyledTextCtrl?

smallEdit.py (5.34 KB)

sample.txt (175 Bytes)

Xtd00 wrote:

A simple text editor with wx.Frame, StyledTextCtrl, and some basic menue
items : As File menue; Open, Save, Save As, Close : as Edit menue; Copy,
Cut, Paste, Undo, Redo.

Operations are just open file, do something, and close. Quite simple.

If the data in the StyledTextCtrl is changed, I want to open a dialog
box (ShowModal()) to confirm save or not by both the Xbox of the
wx.Frame and the Close(Cmd-W) menu.

To get whether the data is changed or not, when the stc.EVT_STC_CHANGE
is sent to the application, the variable I set as 'flagDirty'=True.(This
editor doesn't need to save the Style change, so stc.EVT_STC_CHANGE, not
stc.EVT_STC_MODIFIED.)

I bind in the wx.Frame's __init__(self, title) function, I bound the
Close event to handler.
self.Bind(wx.EVT_CLOSE, self.OnClose)

And the Close menuitem
menu_File_Close = menu_File.Append(wx.ID_CLOSE, "&Close"+"\t"+"Ctrl+W",
"Close")

Xbox seems work all right.

The menuitem Close is the problem. The dialog displayed OK. But when
No(= wx.ID_NO );

THE PROBLEMS ARE::
1 At the first time pushed, the dialog doesn't work. Second time, dialog
closes, but the wx.Frame doesn't close.
Why dialog works funny like this?

2 Isn't there BETTER WAY for the case like this?
For example, I'm not sure about setting variable like 'flagDirty' is
good for getting whether the data is changed.

3 When just open the window and close without editing, the dialog box
opens for the variable is True. What's wrong with the setting of the
StyledTextCtrl?

The STC has an IsModified method that will return true if the document is different than when it was last saved. When you save the document then you should call SetSavePoint so it knows at which point in the undo history the file was saved.

The typical way to handle closing a frame with an open document goes something like this:

* The event handler for your File->Close menu item should simply do self.Close() where self is the frame.

* Add an event handler for EVT_CLOSE that does something like this (pseudocode):

     if event.CanVeto() and documentIsModified:
        answer = wx.MessageBox("File is not saved, save before exiting?",
                               "Confirm Close", wx.YES_NO|wx.CANCEL)
        if answer == wx.CANCEL:
            event.Veto()
            return
        if answer == wx.YES:
     self.saveTheFile()
     self.Destroy()

···

--
Robin Dunn
Software Craftsman

THANK YOU FOR ADVICE!! And excuse me again, for I didn’t notice that you gave the answer here.

I think I have to get used to use this mailing list. Give me some days.

== Any Way Thank You ===