#!/usr/bin/env python # wxE # a text editor in wxPython # Barret Rennie, 2007 # Public Domain import wx import os.path ID_ABOUT = 1000 ID_NEW = 1001 ID_SAVE = 1002 ID_OPEN = 1003 ID_EXIT = 1004 ID_SAVEAS = 1005 class Window(wx.Frame): openfile = "" openfilepath = "" def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(600,400)) self.textbox = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE) self.textbox.SetFont(wx.Font(10, wx.FONTFAMILY_TELETYPE, wx.NORMAL, wx.NORMAL)) self.CreateStatusBar() filemenu = wx.Menu() filemenu.Append(ID_NEW, "&New", "New Document") filemenu.Append(ID_OPEN, "&Open", "Open Document") filemenu.Append(ID_SAVE, "&Save", "Save Document") filemenu.Append(ID_SAVEAS, "S&ave As", "Save Document As") filemenu.AppendSeparator() filemenu.Append(ID_EXIT, "E&xit", "Exit wxE") helpmenu = wx.Menu() helpmenu.Append(ID_ABOUT, "&About", "About wxE") menubar = wx.MenuBar() menubar.Append(filemenu, "&File") menubar.Append(helpmenu, "&Help") wx.EVT_MENU(self, ID_ABOUT, self.about) wx.EVT_MENU(self, ID_EXIT, self.exit) wx.EVT_MENU(self, ID_SAVE, self.save) wx.EVT_MENU(self, ID_OPEN, self.open) wx.EVT_MENU(self, ID_NEW, self.new) wx.EVT_MENU(self, ID_SAVEAS, self.saveas) self.SetMenuBar(menubar) self.Show(True) def about(self, e): d = wx.MessageDialog(self, "wxE\nAn editor written in wxPython\nBarret Rennie, 2007\nPublic Domain", "About wxE", wx.OK) d.ShowModal() d.Destroy() def open(self, e): opend = wx.FileDialog(self, "Choose a file...", self.openfilepath, "", "*", wx.OPEN) if opend.ShowModal() == wx.ID_OK: self.openfilepath = opend.GetDirectory() self.openfile = opend.GetFilename() file = open(os.path.join(self.openfilepath, self.openfile), 'r') self.textbox.SetValue(file.read()) file.close() opend.Destroy() def save(self, e): if not self.openfile: self.saveas(e) else: file = open(os.path.join(self.openfilepath, self.openfile), 'w') file.write(self.textbox.GetValue()) file.close() def saveas(self, e): saved = wx.FileDialog(self, "Save file as...", self.openfilepath, self.openfile, "*", wx.SAVE) if saved.ShowModal() == wx.ID_OK: self.openfilepath = saved.GetDirectory() self.openfile = saved.GetFilename() file = open(os.path.join(self.openfilepath, self.openfile), 'w') file.write(self.textbox.GetValue()) file.close saved.Destroy() def new(self, e): self.openfile = "" self.openfilepath = "" self.textbox.SetValue("") def exit(self, e): self.Close(True) app = wx.PySimpleApp() frame = Window(None, -1, "wxE") app.MainLoop()