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 \
  28             [(wx.ID_ABOUT, '&About', 'Information about this program',
  29                 self.OnAbout),
  30              (wx.ID_OPEN, '&Open', 'Open a new file', self.OnOpen),
  31              (wx.ID_SAVE, '&Save', 'Save the current file', self.OnSave),
  32              (wx.ID_SAVEAS, 'Save &As', 'Save the file under a different name',
  33                 self.OnSaveAs),
  34              (None, None, None, None),
  35              (wx.ID_EXIT, 'E&xit', 'Terminate the program', self.OnExit)]:
  36             if id == None:
  37                 fileMenu.AppendSeparator()
  38             else:
  39                 item = fileMenu.Append(id, label, helpText)
  40                 self.Bind(wx.EVT_MENU, handler, item)
  41 
  42         menuBar = wx.MenuBar()
  43         menuBar.Append(fileMenu, '&File') # Add the fileMenu to the MenuBar
  44         self.SetMenuBar(menuBar)  # Add the menuBar to the Frame
  45 
  46     def SetTitle(self):
  47         # MainWindow.SetTitle overrides wx.Frame.SetTitle, so we have to
  48         # call it using super:
  49         super(MainWindow, self).SetTitle('Editor %s'%self.filename)
  50 
  51 
  52     # Helper methods:
  53 
  54     def defaultFileDialogOptions(self):
  55         ''' Return a dictionary with file dialog options that can be
  56             used in both the save file dialog as well as in the open
  57             file dialog. '''
  58         return dict(message='Choose a file', defaultDir=self.dirname,
  59                     wildcard='*.*')
  60 
  61     def askUserForFilename(self, **dialogOptions):
  62         dialog = wx.FileDialog(self, **dialogOptions)
  63         if dialog.ShowModal() == wx.ID_OK:
  64             userProvidedFilename = True
  65             self.filename = dialog.GetFilename()
  66             self.dirname = dialog.GetDirectory()
  67             self.SetTitle() # Update the window title with the new filename
  68         else:
  69             userProvidedFilename = False
  70         dialog.Destroy()
  71         return userProvidedFilename
  72 
  73     # Event handlers:
  74 
  75     def OnAbout(self, event):
  76         dialog = wx.MessageDialog(self, 'A sample editor\n'
  77             'in wxPython', 'About Sample Editor', wx.OK)
  78         dialog.ShowModal()
  79         dialog.Destroy()
  80 
  81     def OnExit(self, event):
  82         self.Close()  # Close the main window.
  83 
  84     def OnSave(self, event):
  85         textfile = open(os.path.join(self.dirname, self.filename), 'w')
  86         textfile.write(self.control.GetValue())
  87         textfile.close()
  88 
  89     def OnOpen(self, event):
  90         if self.askUserForFilename(style=wx.OPEN,
  91                                    **self.defaultFileDialogOptions()):
  92             textfile = open(os.path.join(self.dirname, self.filename), 'r')
  93             self.control.SetValue(textfile.read())
  94             textfile.close()
  95 
  96     def OnSaveAs(self, event):
  97         if self.askUserForFilename(defaultFile=self.filename, style=wx.SAVE,
  98                                    **self.defaultFileDialogOptions()):
  99             self.OnSave(event)
 100 
 101 
 102 app = wx.App()
 103 frame = MainWindow()
 104 frame.Show()
 105 app.MainLoop()

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

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