A good way to close the frame after confirm if save data or not

A kind of small text editor with only minimal functions.
The wx.Frame which is the base, a StyledTextCtrl and basic menu items like “Open, Save, Save As, Close” and “Cup, Copy, Paste, Undo, Redo”.
Basically the operation is: Open a file, edit something, and Close. Quite simple.

When Close, if the data in the StyledTextCtrl is edited, I want to confirm the user ‘Save or not’ (Dialog box as a modal).

If No, just close the wx.Frame. If Yes, Open the file save dialog and save.

Whether the data is changed is got from stc.EVT_STC_CHANGE. I made a variable ‘flagDirty’ and put the value to reference.

(I don’t have to save the Style changes about this editor, so stc.EVT_STC_CHANGE instead of stc.EVT_STC_MODIFIED.)

I have a PROBLEM when Closing the wx.Frame.

The base wx.Frame has the X box to close. I want to open the dialog to confirm save or not if the data is changed, so I bind the frame to OnClose handler in the init(self, title).

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

The X box seems to works.

About the Close menu.

self.Bind(wx.EVT_MENU, self.OnClose, menu_File_Close)

This is a common way to bind a menuitem to handler.

THE TROUBLES ARE:

About the handler OnClose.

def OnClose(self, event):

if self.flagDirty == True: # flagDirty True if the stc.EVT_STC_CHANGE is sent to the application

self.confirmDlg(event)

else:

self.Close()

Dialog is displayed all right, but when ‘No’ (=wx.ID_NO), when I first pressed dialog doesn’t work, and second time, dialog closes, but the frame doesn’t close.

1 WHY is the dialog works like this?

2 I just opened the file by the StyledTextCtrl. NOT edited or changet at all, but the Dialog opens.

The variable ‘’ flagDirty" is True from the first time when printed. So I’m not sure if this way is good to see whether the data is edited.

Isn’t there BETTER WAYS for the case like this?

smallEdit.py (5.32 KB)

sample.txt (175 Bytes)

Sorry, I first consult to the group so I didn’t know that it takes time to be displayed after uploading.

So, I uploaded almost the same thing twice.

This topic is the same with “Close the wx.Frame with a StyledTextCtrl after confirming Save or not by a dialog box” next to this.

EXCUSE ME.

Xtd00 wrote:

Sorry, I first consult to the group so I didn't know that it takes time
to be displayed after uploading.

So, I uploaded almost the same thing twice.

This topic is the same with "Close the wx.Frame with a StyledTextCtrl
after confirming Save or not by a dialog box" next to this.

The first time you post to the list your message will need to be confirmed by a human to make sure you are not a robot. Now that you have had your initial posts approved it will be as fast as expected for your messages to get through, (as long as google is not having networking troubles again.)

···

--
Robin Dunn
Software Craftsman

The first time you post to the list your message will need to be

confirmed by a human to make sure you are not a robot. Now that you
have had your initial posts approved it will be as fast as expected for
your messages to get through, (as long as google is not having
networking troubles again.)

Mr. Robin Dunn,

I’m not a robot. Do I seem like a robot?! What was the reason that you thought I’m a robot???

I consult to this group for I really want some advice.

You didn’t pay attention to what he said. This is a world-wide
mailing lists, and every world-wide mailing list these days is a
huge target for spammers and scammers. I run about a dozen myself,
so I’m well aware of the problem. These slimeballs have automated
programs that sign up on mailing lists and blast out their hateful
spam, hoping there will be no controls.
To prevent that, the first time ANYONE posts to this list, the
posting will be held for moderator approval. After the moderator
approves it, all further posts will go through automatically.
So, the fact that your post was approved says that Robin decided you
were NOT a robot.

···

Xtd00 wrote:

    The first time you post to the list your message

will need to be

      confirmed

by a human to make sure you are not a robot. Now that you

      have had your initial posts approved it will be as fast as

expected for

      your messages to get through, (as long as google is not having


      networking troubles again.)

Mr. Robin Dunn,

      I'm not a robot.  Do I seem like a robot?!  What was the

reason that you thought I’m a robot???

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Thank you, Mr. Tim Roberts. I understand about the situations.

Several times I did Cmd+W, I got the following error message:

File “C:\file_path_here.py”, line 150, in OnClose

if self.flagDirty == True:

RuntimeError: maximum recursion depth exceeded in cmp

↑ What is the ‘recursion depth’ means here?

it means that you have a function calling itself over and over again.

I suspect that you have method bound to EVT_CLOSE that then triggers a new
CLOSE event, which calls your method, which triggers a .... etc.

Without seeing the code, I can't suggest how to fix it thought. Try:

http://wiki.wxpython.org/MakingSampleApps

-Chris

···

On Sat, Sep 28, 2013 at 4:32 AM, Xtd00 <jet8e5x@gmail.com> wrote:

Several times I did Cmd+W, I got the following error message:

File "C:\file_path_here.py", line 150, in OnClose
    if self.flagDirty == True:
RuntimeError: maximum recursion depth exceeded in cmp

  ↑ What is the 'recursion depth' means here?

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov