1 import wx
   2 import os.path
   3 
   4 
   5 class MainWindow(wx.Frame):
   6     def __init__(self, filename='noname.txt'):
   7         super(MainWindow, self).__init__(None, size=(400,200))
   8         self.filename = filename
   9         self.dirname = '.'
  10         self.CreateInteriorWindowComponents()
  11         self.CreateExteriorWindowComponents()
  12 
  13     def CreateInteriorWindowComponents(self):
  14         ''' Create "interior" window components. In this case it is just a
  15             simple multiline text control. '''
  16         self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
  17 
  18     def CreateExteriorWindowComponents(self):
  19         ''' Create "exterior" window components, such as menu and status
  20             bar. '''
  21         self.CreateMenu()
  22         self.CreateStatusBar()
  23         self.SetTitle()
  24 
  25     def CreateMenu(self):
  26         fileMenu = wx.Menu()
  27         for id, label, helpText, handler in \
            [(wx.ID_ABOUT, '&About', 'Information about this program',
  28                 self.OnAbout),
  29              (wx.ID_OPEN, '&Open', 'Open a new file', self.OnOpen),
  30              (wx.ID_SAVE, '&Save', 'Save the current file', self.OnSave),
  31              (wx.ID_SAVEAS, 'Save &As', 'Save the file under a different name',
  32                 self.OnSaveAs),
  33              (None, None, None, None),
  34              (wx.ID_EXIT, 'E&xit', 'Terminate the program', self.OnExit)]:
  35             if id == None:
  36                 fileMenu.AppendSeparator()
  37             else:
  38                 item = fileMenu.Append(id, label, helpText)
  39                 self.Bind(wx.EVT_MENU, handler, item)
  40 
  41         menuBar = wx.MenuBar()
  42         menuBar.Append(fileMenu, '&File') # Add the fileMenu to the MenuBar
  43         self.SetMenuBar(menuBar)  # Add the menuBar to the Frame
  44 
  45     def SetTitle(self):
  46         # MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have to
  47         # call it using super:
  48         super(MainWindow, self).SetTitle('Editor %s'%self.filename)
  49 
  50 
  51     # Helper methods:
  52 
  53     def defaultFileDialogOptions(self):
  54         ''' Return a dictionary with file dialog options that can be
  55             used in both the save file dialog as well as in the open
  56             file dialog. '''
  57         return dict(message='Choose a file', defaultDir=self.dirname,
  58                     wildcard='*.*')
  59 
  60     def askUserForFilename(self, **dialogOptions):
  61         dialog = wx.FileDialog(self, **dialogOptions)
  62         if dialog.ShowModal() == wx.ID_OK:
  63             userProvidedFilename = True
  64             self.filename = dialog.GetFilename()
  65             self.dirname = dialog.GetDirectory()
  66             self.SetTitle() # Update the window title with the new filename
  67         else:
  68             userProvidedFilename = False
  69         dialog.Destroy()
  70         return userProvidedFilename
  71 
  72     # Event handlers:
  73 
  74     def OnAbout(self, event):
  75         dialog = wx.MessageDialog(self, 'A sample editor\n'
  76             'in wxPython', 'About Sample Editor', wx.OK)
  77         dialog.ShowModal()
  78         dialog.Destroy()
  79 
  80     def OnExit(self, event):
  81         self.Close()  # Close the main window.
  82 
  83     def OnSave(self, event):
  84         textfile = open(os.path.join(self.dirname, self.filename), 'w')
  85         textfile.write(self.control.GetValue())
  86         textfile.close()
  87 
  88     def OnOpen(self, event):
  89         if self.askUserForFilename(style=wx.OPEN,
  90                                    **self.defaultFileDialogOptions()):
  91             textfile = open(os.path.join(self.dirname, self.filename), 'r')
  92             self.control.SetValue(textfile.read())
  93             textfile.close()
  94 
  95     def OnSaveAs(self, event):
  96         if self.askUserForFilename(defaultFile=self.filename, style=wx.SAVE,
  97                                    **self.defaultFileDialogOptions()):
  98             self.OnSave(event)
  99 
 100 
 101 app = wx.App()
 102 frame = MainWindow()
 103 frame.Show()
 104 app.MainLoop()

WxHowtoSmallEditor (last edited 2008-03-11 10:50:29 by localhost)