Create a simple text editor (Phoenix)

Keywords : Editor, Text, TextCtrl, Stc, StyledTextCtrl.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

   1 # sample_one.py
   2 
   3 import sys
   4 import os.path
   5 import wx
   6 
   7 # class MyFrame
   8 # class MyApp
   9 
  10 #-------------------------------------------------------------------------------
  11 
  12 # This is how you pre-establish a file filter so that the
  13 # dialog only shows the extension(s) you want it to.
  14 
  15 wildcard = "Text (*.txt)|*.txt|"        \
  16            "Executable (*.exe)|*.exe|"  \
  17            "Library (*.dll)|*.dll|"     \
  18            "Driver (*.sys)|*.sys|"      \
  19            "ActiveX (*.ocx)|*.ocx|"     \
  20            "Python (*.py)|*.py|"        \
  21            "Python (*.pyw)|*.pyw|"      \
  22            "All (*.*)|*.*"
  23 
  24 #-------------------------------------------------------------------------------
  25 
  26 class MyFrame(wx.Frame):
  27     def __init__(self, filename="noname.txt"):
  28         super(MyFrame, self).__init__(None, size=(400, 300))
  29 
  30         #------------
  31 
  32         # Return icons folder.
  33         self.icons_dir = wx.GetApp().GetIconsDir()
  34 
  35         #------------
  36 
  37         self.filename = filename
  38         self.dirname  = "."
  39 
  40         #------------
  41 
  42         # Simplified init method.
  43         self.CreateInteriorWindowComponents()
  44         self.CreateExteriorWindowComponents()
  45 
  46         #------------
  47 
  48         self.CenterOnScreen()
  49 
  50     #---------------------------------------------------------------------------
  51 
  52     def SetTitle(self):
  53         """
  54         ...
  55         """
  56 
  57         # MyFrame.SetTitle overrides wx.Frame.SetTitle,
  58         # so we have to call it using super :
  59         super(MyFrame, self).SetTitle("Editor %s" % self.filename)
  60 
  61 
  62     def CreateInteriorWindowComponents(self):
  63         """
  64         Create "interior" window components. In this case
  65         it is just a simple multiline text control.
  66         """
  67 
  68         self.control = wx.TextCtrl(self, -1, value="", style=wx.TE_MULTILINE)
  69 
  70 
  71     def CreateExteriorWindowComponents(self):
  72         """
  73         Create "exterior" window components, such as menu and status bar.
  74         """
  75 
  76         # Simplified init method.
  77         self.SetTitle()
  78 
  79         #------------
  80 
  81         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  82                                          "icon_wxWidgets.ico"),
  83                             type=wx.BITMAP_TYPE_ICO)
  84         self.SetIcon(frameIcon)
  85 
  86         #------------
  87 
  88         self.CreateMenu()
  89         self.CreateStatusBar()
  90         self.BindEvents()
  91 
  92 
  93     def CreateMenu(self):
  94         """
  95         Create menu and menu bar.
  96         """
  97 
  98         menuBar = wx.MenuBar()
  99 
 100         #------------
 101 
 102         fileMenu = wx.Menu()
 103 
 104         for id, label, helpText, handler in \
 105             [(wx.ID_ABOUT, "&About",
 106               "Information about this program.", self.OnAbout),
 107              (None, None, None, None),
 108              (wx.ID_OPEN, "&Open",
 109               "Open a new file.", self.OnOpen),
 110              (wx.ID_SAVE, "&Save",
 111               "Save the current file.", self.OnSave),
 112              (wx.ID_SAVEAS, "Save &as",
 113               "Save the file under a different name.", self.OnSaveAs),
 114              (None, None, None, None),
 115              (wx.ID_EXIT, "E&xit",
 116               "Terminate the program.", self.OnCloseMe)]:
 117 
 118             if id == None:
 119                 fileMenu.AppendSeparator()
 120             else:
 121                 item = fileMenu.Append(id, label, helpText)
 122 
 123                 #------------
 124 
 125                 # Bind some events to an events handler.
 126                 self.Bind(wx.EVT_MENU, handler, item)
 127 
 128         #------------
 129 
 130         # Add the fileMenu to the menuBar.
 131         menuBar.Append(fileMenu, "&File")
 132 
 133         #------------
 134 
 135         # Add the menuBar to the frame.
 136         self.SetMenuBar(menuBar)
 137 
 138 
 139     def BindEvents(self):
 140         """
 141         ...
 142         """
 143 
 144         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 145 
 146 
 147     def DefaultFileDialogOptions(self):
 148         """
 149         Return a dictionary with file dialog options that can be
 150         used in both the save file dialog as well as in the open
 151         file dialog.
 152         """
 153 
 154         return dict(message="Choose a file :",
 155                     defaultDir=self.dirname,
 156                     wildcard=wildcard)
 157 
 158 
 159     def AskUserForFilename(self, **dialogOptions):
 160         """
 161         ...
 162         """
 163 
 164         dialog = wx.FileDialog(self, **dialogOptions)
 165 
 166         if dialog.ShowModal() == wx.ID_OK:
 167             userProvidedFilename = True
 168             self.filename = dialog.GetFilename()
 169             self.dirname = dialog.GetDirectory()
 170             # Update the window title with the new filename.
 171             self.SetTitle()
 172         else:
 173             userProvidedFilename = False
 174 
 175         dialog.Destroy()
 176 
 177         return userProvidedFilename
 178 
 179 
 180     def OnOpen(self, event):
 181         """
 182         Open file.
 183         """
 184 
 185         if self.AskUserForFilename(style=wx.FD_OPEN,
 186                                    **self.DefaultFileDialogOptions()):
 187             file = open(os.path.join(self.dirname, self.filename), 'r', encoding='utf-8')
 188             self.control.SetValue(file.read())
 189             file.close()
 190 
 191 
 192     def OnSave(self, event):
 193         """
 194         Save file.
 195         """
 196 
 197         with open(os.path.join(self.dirname, self.filename), 'w', encoding='utf-8') as file:
 198             file.write(self.control.GetValue())
 199 
 200 
 201     def OnSaveAs(self, event):
 202         """
 203         Save file as.
 204         """
 205 
 206         if self.AskUserForFilename(defaultFile=self.filename, style=wx.FD_SAVE,
 207                                    **self.DefaultFileDialogOptions()):
 208             self.OnSave(event)
 209 
 210 
 211     def OnAbout(self, event):
 212         """
 213         About dialog.
 214         """
 215 
 216         dialog = wx.MessageDialog(self,
 217                                   "A sample editor in wxPython.",
 218                                   "About sample editor",
 219                                   wx.OK)
 220         dialog.ShowModal()
 221         dialog.Destroy()
 222 
 223 
 224     def OnCloseMe(self, event):
 225         """
 226         Close the main window.
 227         """
 228 
 229         self.Close(True)
 230 
 231 
 232     def OnCloseWindow(self, event):
 233         """
 234         Quit and destroy application.
 235         """
 236 
 237         self.Destroy()
 238 
 239 #-------------------------------------------------------------------------------
 240 
 241 class MyApp(wx.App):
 242     """
 243     ....
 244     """
 245     def OnInit(self):
 246 
 247         #------------
 248 
 249         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 250 
 251         #------------
 252 
 253         frame = MyFrame()
 254         self.SetTopWindow(frame)
 255         frame.Show(True)
 256 
 257         return True
 258 
 259     #---------------------------------------------------------------------------
 260 
 261     def GetInstallDir(self):
 262         """
 263         Returns the installation directory for my application.
 264         """
 265 
 266         return self.installDir
 267 
 268 
 269     def GetIconsDir(self):
 270         """
 271         Returns the icons directory for my application.
 272         """
 273 
 274         icons_dir = os.path.join(self.installDir, "icons")
 275         return icons_dir
 276 
 277 #-------------------------------------------------------------------------------
 278 
 279 def main():
 280     app = MyApp(False)
 281     app.MainLoop()
 282 
 283 #-------------------------------------------------------------------------------
 284 
 285 if __name__ == "__main__" :
 286     main()


Sample two

img_sample_two.png

   1 # sample_two.py
   2 
   3 import sys
   4 import os.path
   5 import wx
   6 import wx.stc as stc   # StyledTextCtrl
   7 
   8 # class MyFrame
   9 # class MyApp
  10 
  11 #-------------------------------------------------------------------------------
  12 
  13 # This is how you pre-establish a file filter so that the
  14 # dialog only shows the extension(s) you want it to.
  15 
  16 wildcard = "Text (*.txt)|*.txt|"        \
  17            "Config (*.cfg)|*.cfg|"      \
  18            "Python (*.py)|*.py|"        \
  19            "Python (*.pyw)|*.pyw|"      \
  20            "All (*.*)|*.*"
  21 
  22 #-------------------------------------------------------------------------------
  23 
  24 class MyFrame(wx.Frame):
  25     def __init__(self, filename="noname.txt"):
  26         super(MyFrame, self).__init__(None, size=(400, 300))
  27 
  28         #------------
  29 
  30         # Return icons folder.
  31         self.icons_dir = wx.GetApp().GetIconsDir()
  32 
  33         #------------
  34 
  35         self.filename = filename
  36         self.dirname  = "."
  37         self.flagDirty = False
  38 
  39         #------------
  40 
  41         # Simplified init method.
  42         self.SetProperties()
  43         self.CreateMenuBar()
  44         self.CreateStyledTextControl()
  45         self.CreateStatusBar()
  46         self.BindEvents()
  47 
  48     #---------------------------------------------------------------------------
  49 
  50     def SetProperties(self):
  51         """
  52         ...
  53         """
  54 
  55         self.SetTitle()
  56 
  57         #------------
  58 
  59         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  60                                          "icon_wxWidgets.ico"),
  61                             type=wx.BITMAP_TYPE_ICO)
  62         self.SetIcon(frameIcon)
  63 
  64 
  65     def SetTitle(self):
  66         """
  67         ...
  68         """
  69 
  70         # MyFrame.SetTitle overrides wx.Frame.SetTitle,
  71         # so we have to call it using super :
  72         super(MyFrame, self).SetTitle("Editor %s" % self.filename)
  73 
  74 
  75     def CreateMenuBar(self):
  76         """
  77         Create menu and menu bar.
  78         """
  79 
  80         menuBar = wx.MenuBar()
  81 
  82         #------------
  83 
  84         menu_File = wx.Menu()
  85         menu_Edit = wx.Menu()
  86         menu_About = wx.Menu()
  87 
  88         #------------
  89         #------------
  90 
  91         self.menu_File_Open = menu_File.Append(wx.ID_OPEN,
  92                                                "&Open" + "\t" + "Ctrl+O",
  93                                                "Open a new file.")
  94         #------------
  95 
  96         menu_File.AppendSeparator()
  97 
  98         #------------
  99 
 100         self.menu_File_Save = menu_File.Append(wx.ID_SAVE,
 101                                                "&Save" + "\t" + "Ctrl+S",
 102                                                "Save the current file.")
 103         self.menu_File_SaveAs = menu_File.Append(wx.ID_SAVEAS,
 104                                                  "&Save as" + "\t" + "Ctrl+Shift+S",
 105                                                  "Save the file under a different name.")
 106 
 107         #------------
 108 
 109         menu_File.AppendSeparator()
 110 
 111         #------------
 112 
 113         self.menu_File_Close = menu_File.Append(wx.ID_EXIT,
 114                                                 "&Exit" + "\t" + "Ctrl+X",
 115                                                 "Exit the program.")
 116         #------------
 117 
 118         self.menu_Edit_Cut = menu_Edit.Append(wx.ID_CUT,
 119                                               "&Cut" + "\t" + "Ctrl+X",
 120                                               "Cut")
 121         self.menu_Edit_Copy = menu_Edit.Append(wx.ID_COPY,
 122                                                "&Copy" + "\t" + "Ctrl+C",
 123                                                "Copy")
 124         self.menu_Edit_Paste = menu_Edit.Append(wx.ID_PASTE,
 125                                                 "&Paste" + "\t" + "Ctrl+V",
 126                                                 "Paste")
 127         #------------
 128 
 129         menu_Edit.AppendSeparator()
 130 
 131         #------------
 132 
 133         self.menu_Edit_Undo = menu_Edit.Append(wx.ID_UNDO,
 134                                                "&Undo" + "\t" + "Ctrl+Z",
 135                                                "Undo")
 136         self.menu_Edit_Redo = menu_Edit.Append(wx.ID_REDO,
 137                                                "&Redo" + "\t" + "Ctrl+Shift+Z",
 138                                                "Redo")
 139 
 140         #------------
 141 
 142         self.menu_About_Info = menu_About.Append(wx.ID_ABOUT,
 143                                                  "&About" + "\t" + "Ctrl+I",
 144                                                  "Information about this program.")
 145 
 146         #------------
 147         #------------
 148 
 149         menuBar.Append(menu_File, "File")
 150         menuBar.Append(menu_Edit, "Edit")
 151         menuBar.Append(menu_About, "About")
 152 
 153         #------------
 154 
 155         self.SetMenuBar(menuBar)
 156 
 157 
 158     def CreateStyledTextControl(self):
 159         """
 160         ...
 161         """
 162 
 163         self.St_TextCtrl = stc.StyledTextCtrl(self)
 164         self.St_TextCtrl.SetWindowStyle(self.St_TextCtrl.GetWindowStyle() | wx.DOUBLE_BORDER)
 165         self.St_TextCtrl.StyleSetSpec(stc.STC_STYLE_DEFAULT, "size:10,face:Courier New")
 166         self.St_TextCtrl.SetWrapMode(stc.STC_WRAP_WORD)
 167 
 168         #------------
 169 
 170         layout = wx.BoxSizer(wx.HORIZONTAL)
 171         layout.Add(self.St_TextCtrl, proportion=1, border=0, flag=wx.ALL|wx.EXPAND)
 172         self.SetSizer(layout)
 173 
 174         #------------
 175 
 176         self.St_TextCtrl.Bind(stc.EVT_STC_CHANGE, self.OnChangeTxtCtrl)
 177 
 178 
 179     def BindEvents(self):
 180         """
 181         ...
 182         """
 183 
 184         self.Bind(wx.EVT_MENU, self.OnOpen, self.menu_File_Open)
 185         self.Bind(wx.EVT_MENU, self.OnSave, self.menu_File_Save)
 186         self.Bind(wx.EVT_MENU, self.OnSaveAs, self.menu_File_SaveAs)
 187         self.Bind(wx.EVT_MENU, self.OnCloseMe, self.menu_File_Close)
 188         self.Bind(wx.EVT_MENU, self.OnCut, self.menu_Edit_Cut)
 189         self.Bind(wx.EVT_MENU, self.OnCopy, self.menu_Edit_Copy)
 190         self.Bind(wx.EVT_MENU, self.OnPaste, self.menu_Edit_Paste)
 191         self.Bind(wx.EVT_MENU, self.OnUndo, self.menu_Edit_Undo)
 192         self.Bind(wx.EVT_MENU, self.OnRedo, self.menu_Edit_Redo)
 193         self.Bind(wx.EVT_MENU, self.OnAbout, self.menu_About_Info)
 194 
 195         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 196 
 197 
 198     def OnChangeTxtCtrl(self, event):
 199         """
 200         ...
 201         """
 202 
 203         lines = self.St_TextCtrl.GetLineCount()
 204         width = self.St_TextCtrl.TextWidth(stc.STC_STYLE_LINENUMBER, str(lines) + " ")
 205         self.St_TextCtrl.SetMarginWidth(0, width)
 206 
 207         self.flagDirty = True
 208 
 209 
 210     def DefaultFileDialogOptions(self):
 211         """
 212         Return a dictionary with file dialog options that can be
 213         used in both the save file dialog as well as in the open
 214         file dialog.
 215         """
 216 
 217         return dict(message="Choose a file :",
 218                     defaultDir=self.dirname,
 219                     wildcard=wildcard)
 220 
 221 
 222     def AskUserForFilename(self, **dialogOptions):
 223         """
 224         ...
 225         """
 226 
 227         dialog = wx.FileDialog(self, **dialogOptions)
 228 
 229         if dialog.ShowModal() == wx.ID_OK:
 230             userProvidedFilename = True
 231             self.filename = dialog.GetFilename()
 232             self.dirname = dialog.GetDirectory()
 233             # Update the window title with the new filename.
 234             self.SetTitle()
 235         else:
 236             userProvidedFilename = False
 237 
 238         dialog.Destroy()
 239 
 240         return userProvidedFilename
 241 
 242 
 243     def OnOpen(self, event):
 244         """
 245         Open file.
 246         """
 247 
 248         if self.AskUserForFilename(style=wx.FD_OPEN,
 249                                    **self.DefaultFileDialogOptions()):
 250             file = open(os.path.join(self.dirname, self.filename), 'r', encoding='utf-8')
 251             self.St_TextCtrl.SetValue(file.read())
 252             file.close()
 253 
 254 
 255     def OnSave(self, event):
 256         """
 257         Save file.
 258         """
 259 
 260         with open(os.path.join(self.dirname, self.filename), 'w', encoding='utf-8') as file:
 261             file.write(self.St_TextCtrl.GetValue())
 262 
 263 
 264     def OnSaveAs(self, event):
 265         """
 266         Save file as.
 267         """
 268 
 269         if self.AskUserForFilename(defaultFile=self.filename, style=wx.FD_SAVE,
 270                                    **self.DefaultFileDialogOptions()):
 271             self.OnSave(event)
 272 
 273 
 274     def OnCut(self, event):
 275         """
 276         ...
 277         """
 278 
 279         self.St_TextCtrl.Cut()
 280 
 281 
 282     def OnCopy(self, event):
 283         """
 284         ...
 285         """
 286 
 287         self.St_TextCtrl.Copy()
 288 
 289 
 290     def OnPaste(self, event):
 291         """
 292         ...
 293         """
 294 
 295         self.St_TextCtrl.Paste()
 296 
 297 
 298     def OnUndo(self, event):
 299         """
 300         ...
 301         """
 302 
 303         self.St_TextCtrl.Undo()
 304 
 305 
 306     def OnRedo(self, event):
 307         """
 308         ...
 309         """
 310 
 311         self.St_TextCtrl.Redo()
 312 
 313 
 314     def OnAbout(self, event):
 315         """
 316         About dialog.
 317         """
 318 
 319         dialog = wx.MessageDialog(self,
 320                                   "A sample StyledTextCtrl editor in wxPython.",
 321                                   "About sample editor",
 322                                   wx.OK)
 323         dialog.ShowModal()
 324         dialog.Destroy()
 325 
 326 
 327     def OnCloseMe(self, event):
 328         """
 329         Close the main window.
 330         """
 331 
 332         self.Close(True)
 333 
 334 
 335     def OnCloseWindow(self, event):
 336         """
 337         Quit and destroy application.
 338         """
 339 
 340         self.Destroy()
 341 
 342 #-------------------------------------------------------------------------------
 343 
 344 class MyApp(wx.App):
 345     """
 346     ....
 347     """
 348     def OnInit(self):
 349 
 350         #------------
 351 
 352         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 353 
 354         #------------
 355 
 356         frame = MyFrame()
 357         self.SetTopWindow(frame)
 358         frame.Show(True)
 359 
 360         return True
 361 
 362     #---------------------------------------------------------------------------
 363 
 364     def GetInstallDir(self):
 365         """
 366         Returns the installation directory for my application.
 367         """
 368 
 369         return self.installDir
 370 
 371 
 372     def GetIconsDir(self):
 373         """
 374         Returns the icons directory for my application.
 375         """
 376 
 377         icons_dir = os.path.join(self.installDir, "icons")
 378         return icons_dir
 379 
 380 #-------------------------------------------------------------------------------
 381 
 382 def main():
 383     app = MyApp(False)
 384     app.MainLoop()
 385 
 386 #-------------------------------------------------------------------------------
 387 
 388 if __name__ == "__main__" :
 389     main()


Sample three

img_sample_three.png

   1 # sample_three.py
   2 
   3 """
   4 
   5 Author : Jan Bodnar / Zetcode
   6 Created : Apr., 2007
   7 Updated : Nov. 16, 2020 by Ecco
   8 Link : https://wiki.wxpython.org/Another%20tutorial%20%28Phoenix%29#Editor
   9 
  10 """
  11 
  12 import wx
  13 import os
  14 
  15 TBFLAGS = ( wx.TB_HORIZONTAL
  16             | wx.NO_BORDER
  17             | wx.TB_FLAT
  18             #| wx.TB_TEXT
  19             #| wx.TB_HORZ_LAYOUT
  20             )
  21 
  22 # This is how you pre-establish a file filter so that the dialog
  23 # only shows the extension(s) you want it to.
  24 wildcard = "Python source (*.py)|*.py|"     \
  25            "Compiled Python (*.pyc)|*.pyc|" \
  26            "SPAM files (*.spam)|*.spam|"    \
  27            "Egg file (*.egg)|*.egg|"        \
  28            "All files (*.*)|*.*"
  29 
  30 #---------------------------------------------------------------------------
  31 
  32 class Editor(wx.Frame):
  33     def __init__(self, parent, id, title):
  34         wx.Frame.__init__(self, parent, id, title, size=(400, 300))
  35 
  36         #------------
  37 
  38         icon = wx.Icon("./icons/icon_wxWidgets.ico")
  39         self.SetIcon(icon)
  40 
  41         #------------
  42         
  43         # Variables.
  44         self.modify = False
  45         self.last_name_saved = ''
  46         self.replace = False
  47 
  48         #------------
  49         
  50         # Setting up menubar.
  51         menubar = wx.MenuBar()
  52 
  53         file = wx.Menu()
  54         new = wx.MenuItem(file, 101, '&New\tCtrl+N', 'Creates a new document.')
  55         new.SetBitmap(wx.Bitmap('./images/stock_new-16.png'))
  56         file.Append(new)
  57 
  58         open = wx.MenuItem(file, 102, '&Open\tCtrl+O', 'Open an existing file.')
  59         open.SetBitmap(wx.Bitmap('./images/stock_open-16.png'))
  60         file.Append(open)
  61         file.AppendSeparator()
  62 
  63         save = wx.MenuItem(file, 103, '&Save\tCtrl+S', 'Save the file.')
  64         save.SetBitmap(wx.Bitmap('./images/stock_save-16.png'))
  65         file.Append(save)
  66 
  67         saveas = wx.MenuItem(file, 104, 'Save &as...\tShift+Ctrl+S', 'Save the file with a different name.')
  68         saveas.SetBitmap(wx.Bitmap('./images/stock_save_as-16.png'))
  69         file.Append(saveas)
  70         file.AppendSeparator()
  71 
  72         quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the application.')
  73         quit.SetBitmap(wx.Bitmap('./images/stock_exit-16.png'))
  74         file.Append(quit)
  75 
  76         edit = wx.Menu()
  77         cut = wx.MenuItem(edit, 106, 'C&ut\tCtrl+X', 'Cut the selection.')
  78         cut.SetBitmap(wx.Bitmap('./images/stock_cut-16.png'))
  79         edit.Append(cut)
  80 
  81         copy = wx.MenuItem(edit, 107, '&Copy\tCtrl+C', 'Copy the selection.')
  82         copy.SetBitmap(wx.Bitmap('./images/stock_copy-16.png'))
  83         edit.Append(copy)
  84 
  85         paste = wx.MenuItem(edit, 108, '&Paste\tCtrl+V', 'Paste text from clipboard.')
  86         paste.SetBitmap(wx.Bitmap('./images/stock_paste-16.png'))
  87         edit.Append(paste)
  88 
  89         delete = wx.MenuItem(edit, 109, '&Delete', 'Delete the selected text.')
  90         delete.SetBitmap(wx.Bitmap('./images/stock_delete-16.png'))
  91 
  92         edit.Append(delete)
  93         edit.AppendSeparator()
  94         edit.Append(110, 'Select &All\tCtrl+A', 'Select the entire text.')
  95 
  96         view = wx.Menu()
  97         view.Append(111, '&Statusbar', 'Show StatusBar.')
  98 
  99         help = wx.Menu()
 100         about = wx.MenuItem(help, 112, '&About\tF1', 'About Editor.')
 101         about.SetBitmap(wx.Bitmap('./images/stock_about-16.png'))
 102         help.Append(about)
 103 
 104         menubar.Append(file, '&File')
 105         menubar.Append(edit, '&Edit')
 106         menubar.Append(view, '&View')
 107         menubar.Append(help, '&Help')
 108 
 109         self.SetMenuBar(menubar)
 110 
 111         #------------
 112         
 113         # setting up toolbar
 114         self.toolbar = self.CreateToolBar(TBFLAGS)
 115         self.toolbar.AddTool(801, "New", wx.Image('./images/stock_new.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 116                              wx.NullBitmap, wx.ITEM_NORMAL, 'New', "Long help for 'New'", None)
 117         self.toolbar.AddTool(802, "Open", wx.Image('./images/stock_open.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 118                              wx.NullBitmap, wx.ITEM_NORMAL, 'Open', "Long help for 'Open'", None)
 119         self.toolbar.AddTool(803, "Save", wx.Image('./images/stock_save.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 120                              wx.NullBitmap, wx.ITEM_NORMAL, 'Save', "Long help for 'Save'", None)
 121         self.toolbar.AddSeparator()
 122 
 123         self.toolbar.AddTool(804, "Cut", wx.Image('./images/stock_cut.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 124                              wx.NullBitmap, wx.ITEM_NORMAL, 'Cut', "Long help for 'Cut'", None)
 125         self.toolbar.AddTool(805, "Copy", wx.Image('./images/stock_copy.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 126                              wx.NullBitmap, wx.ITEM_NORMAL, 'Copy', "Long help for 'Copy'", None)
 127         self.toolbar.AddTool(806, "Paste", wx.Image('./images/stock_paste.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 128                              wx.NullBitmap, wx.ITEM_NORMAL, 'Paste', "Long help for 'Paste'", None)
 129         self.toolbar.AddSeparator()
 130 
 131         self.toolbar.AddTool(807, "Exit", wx.Image('./images/stock_exit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(),
 132                              wx.NullBitmap, wx.ITEM_NORMAL, 'Exit', "Long help for 'Exit'", None)
 133         
 134         self.toolbar.Realize()
 135 
 136         #------------
 137 
 138         self.text = wx.TextCtrl(self, 1000, '', size=(-1, -1), style=wx.TE_MULTILINE | wx.TE_PROCESS_ENTER)
 139         self.text.SetFocus()
 140 
 141         #------------
 142         
 143         self.text.Bind(wx.EVT_TEXT, self.OnTextChanged, id=1000)
 144         self.text.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
 145         
 146         self.Bind(wx.EVT_MENU, self.NewApplication, id=101)
 147         self.Bind(wx.EVT_MENU, self.OnOpenFile, id=102)
 148         self.Bind(wx.EVT_MENU, self.OnSaveFile, id=103)
 149         self.Bind(wx.EVT_MENU, self.OnSaveAsFile, id=104)
 150         self.Bind(wx.EVT_MENU, self.QuitApplication, id=105)
 151         self.Bind(wx.EVT_MENU, self.OnCut, id=106)
 152         self.Bind(wx.EVT_MENU, self.OnCopy, id=107)
 153         self.Bind(wx.EVT_MENU, self.OnPaste, id=108)
 154         self.Bind(wx.EVT_MENU, self.OnDelete, id=109)
 155         self.Bind(wx.EVT_MENU, self.OnSelectAll, id=110)
 156         self.Bind(wx.EVT_MENU, self.ToggleStatusBar, id=111)
 157         self.Bind(wx.EVT_MENU, self.OnAbout, id=112)
 158         
 159         self.Bind(wx.EVT_TOOL, self.NewApplication, id=801)
 160         self.Bind(wx.EVT_TOOL, self.OnOpenFile, id=802)
 161         self.Bind(wx.EVT_TOOL, self.OnSaveFile, id=803)
 162         self.Bind(wx.EVT_TOOL, self.OnCut, id=804)
 163         self.Bind(wx.EVT_TOOL, self.OnCopy, id=805)
 164         self.Bind(wx.EVT_TOOL, self.OnPaste, id=806)
 165         self.Bind(wx.EVT_TOOL, self.QuitApplication, id=807)
 166 
 167         self.Bind(wx.EVT_CLOSE, self.QuitApplication)
 168         
 169         #------------
 170         
 171         vbox = wx.BoxSizer(wx.VERTICAL)
 172         vbox.Add(self.text, 1, wx.EXPAND, border=0)
 173         
 174         self.SetSizer(vbox)
 175 
 176         #------------
 177 
 178         self.StatusBar()
 179 
 180         #------------
 181         
 182         self.Centre()
 183 
 184         #------------
 185         
 186         self.Show(True)
 187 
 188     #-----------------------------------------------------------------------
 189         
 190     def NewApplication(self, event):
 191         editor = Editor(None, -1, 'Editor')
 192         editor.Centre()
 193         editor.Show()
 194 
 195 
 196     def OnOpenFile(self, event):
 197         file_name = os.path.basename(self.last_name_saved)
 198 
 199         if self.modify:
 200             dlg = wx.MessageDialog(self,
 201                                    'Save changes ?',
 202                                    '',
 203                                    wx.YES_NO |
 204                                    wx.YES_DEFAULT |
 205                                    wx.CANCEL |
 206                                    wx.ICON_QUESTION)
 207             
 208             val = dlg.ShowModal()
 209             if val == wx.ID_YES:
 210                 self.OnSaveFile(event)
 211                 self.DoOpenFile()
 212             elif val == wx.ID_CANCEL:
 213                 dlg.Destroy()
 214             else:
 215                 self.DoOpenFile()
 216         else:
 217             self.DoOpenFile()
 218 
 219 
 220     def DoOpenFile(self):
 221         open_dlg = wx.FileDialog(self,
 222                                  message="Choose a file",
 223                                  defaultDir=os.getcwd(),
 224                                  defaultFile="",
 225                                  wildcard=wildcard,
 226                                  style=wx.FD_OPEN | wx.FD_MULTIPLE |
 227                                        wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST |
 228                                        wx.FD_PREVIEW)
 229         
 230         if open_dlg.ShowModal() == wx.ID_OK:
 231             path = open_dlg.GetPath()
 232 
 233             try:
 234                 file = open(path, 'r')
 235                 text = file.read()
 236                 file.close()
 237                 if self.text.GetLastPosition():
 238                     self.text.Clear()
 239                 self.text.WriteText(text)
 240                 self.last_name_saved = path
 241                 self.statusbar.SetStatusText('', 1)
 242                 self.modify = False
 243 
 244             except IOError as error:
 245                 dlg = wx.MessageDialog(self, 'Error opening file\n' + str(error))
 246                 dlg.ShowModal()
 247 
 248             except UnicodeDecodeError as error:
 249                 dlg = wx.MessageDialog(self, 'Error opening file\n' + str(error))
 250                 dlg.ShowModal()
 251 
 252         open_dlg.Destroy()
 253 
 254 
 255     def OnSaveFile(self, event):
 256         if self.last_name_saved:
 257 
 258             try:
 259                 file = open(self.last_name_saved, 'w')
 260                 text = self.text.GetValue()
 261                 file.write(text)
 262                 file.close()
 263                 self.statusbar.SetStatusText(os.path.basename(self.last_name_saved) + ' Saved', 0)
 264                 self.modify = False
 265                 self.statusbar.SetStatusText('', 1)
 266 
 267             except IOError as error:
 268                 dlg = wx.MessageDialog(self, 'Error saving file\n' + str(error))
 269                 dlg.ShowModal()
 270         else:
 271             self.OnSaveAsFile(event)
 272 
 273 
 274     def OnSaveAsFile(self, event):
 275         save_dlg = wx.FileDialog(self, message="Save file as ...",
 276                                  defaultDir=os.getcwd(),
 277                                  defaultFile="",
 278                                  wildcard=wildcard,
 279                                  style=wx.FD_SAVE |
 280                                        wx.FD_OVERWRITE_PROMPT)
 281         save_dlg.SetFilterIndex(0)
 282         
 283         if save_dlg.ShowModal() == wx.ID_OK:
 284             path = save_dlg.GetPath()
 285 
 286             try:
 287                 file = open(path, 'w')
 288                 text = self.text.GetValue()
 289                 file.write(text)
 290                 file.close()
 291                 self.last_name_saved = os.path.basename(path)
 292                 self.statusbar.SetStatusText(self.last_name_saved + ' Saved', 0)
 293                 self.modify = False
 294                 self.statusbar.SetStatusText('', 1)
 295 
 296             except IOError as error:
 297                 dlg = wx.MessageDialog(self, 'Error saving file\n' + str(error))
 298                 dlg.ShowModal()
 299         save_dlg.Destroy()
 300 
 301 
 302     def OnCut(self, event):
 303         self.text.Cut()
 304 
 305 
 306     def OnCopy(self, event):
 307         self.text.Copy()
 308 
 309 
 310     def OnPaste(self, event):
 311         self.text.Paste()
 312 
 313 
 314     def QuitApplication(self, event):
 315         if self.modify:
 316             dlg = wx.MessageDialog(self, 'Save before Exit ?', '', wx.YES_NO | wx.YES_DEFAULT |
 317                         wx.CANCEL | wx.ICON_QUESTION)
 318             val = dlg.ShowModal()
 319             if val == wx.ID_YES:
 320                 self.OnSaveFile(event)
 321                 if not self.modify:
 322                     wx.Exit()
 323             elif val == wx.ID_CANCEL:
 324                 dlg.Destroy()
 325             else:
 326                 self.Destroy()
 327         else:
 328             self.Destroy()
 329 
 330 
 331     def OnDelete(self, event):
 332         frm, to = self.text.GetSelection()
 333         self.text.Remove(frm, to)
 334 
 335 
 336     def OnSelectAll(self, event):
 337         self.text.SelectAll()
 338 
 339 
 340     def OnTextChanged(self, event):
 341         self.modify = True
 342         self.statusbar.SetStatusText(' Modified', 1)
 343 
 344         event.Skip()
 345 
 346 
 347     def OnKeyDown(self, event):
 348         keycode = event.GetKeyCode()
 349         if keycode == wx.WXK_INSERT:
 350             if not self.replace:
 351                 self.statusbar.SetStatusText('INS', 2)
 352                 self.replace = True
 353             else:
 354                 self.statusbar.SetStatusText('', 2)
 355                 self.replace = False
 356         event.Skip()
 357 
 358 
 359     def ToggleStatusBar(self, event):
 360         if self.statusbar.IsShown():
 361             self.statusbar.Hide()
 362         else:
 363             self.statusbar.Show()
 364         self.SendSizeEvent()
 365 
 366 
 367     def StatusBar(self):
 368         self.statusbar = self.CreateStatusBar()
 369         self.statusbar.SetFieldsCount(3)
 370         self.statusbar.SetStatusWidths([-5, -2, -1])
 371 
 372         
 373     def OnAbout(self, event):
 374         dlg = wx.MessageDialog(self,
 375                                '\tEditor\t\n'\
 376                                '\n'\
 377                                'Another Tutorial\n'\
 378                                'Jan Bodnar 2005-2006\n'\
 379                                'Updated by Ecco 2020.',
 380                                'About Editor',
 381                                wx.OK |
 382                                wx.ICON_INFORMATION)
 383         dlg.ShowModal()
 384         dlg.Destroy()
 385 
 386 #---------------------------------------------------------------------------
 387 
 388 
 389 app = wx.App()
 390 Editor(None, -1, 'Editor')
 391 app.MainLoop()


Download source

source.zip


Additional Information

Link :

https://teratail.com/questions/124859?link=qa_related_pc

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

??? (sample_one.py coding), ??? (sample_two.py coding), Jan Bodnar (sample_three.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

19/07/20 - Ecco (Created page and updated examples for wxPython Phoenix).


Comments

- blah, blah, blah....

Create a simple text editor (Phoenix) (last edited 2020-11-16 15:30:35 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.