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)