这是我以前做的东西,不是很好,但我可以假设你可以随意添加
import os
import wx
class tab(wx.Panel):
def __init__(self, parent, newid=0, name="New Tab", file=None, aNewTab=False):
wx.Panel.__init__(self, parent)
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
sizer = wx.BoxSizer()
sizer.Add(self.control, -1, wx.EXPAND, newid)
self.SetSizer(sizer)
if file != None:
self.control.write(file)
else:
pass
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(1000,900))
self.CreateStatusBar()
self.buttons = []
filemenu = wx.Menu()
helpmenu = wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open a file to edit")
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save the current file")
menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "&Save As", "Save the current file as")
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate the program")
menuAbout = helpmenu.Append(wx.ID_ABOUT, "&About", "Information about this program,")
menuBar = wx.MenuBar()
menuBar.Append(filemenu, "&File")
menuBar.Append(helpmenu, "&Help")
self.SetMenuBar(menuBar)
self.openFiles = { }
self.p = wx.Panel(self)
self.nb = wx.Notebook(self.p)
self.newTab = tab(self.nb)
self.nb.AddPage(self.newTab, "New Tab")
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.p.SetSizer(self.sizer)
#new ids
saveid = wx.NewId()
openid = wx.NewId()
boldid = wx.NewId()
#Set Events
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnSave, menuSaveAs)
# Events that are activated when buttons are pressed
self.Bind(wx.EVT_MENU, self.OnSave, id=saveid)
self.Bind(wx.EVT_MENU, self.OnOpen, id=openid)
self.Bind(wx.EVT_MENU, self.OnBold, id=boldid)
self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('S'), saveid),
(wx.ACCEL_CTRL, ord('O'), openid),
(wx.ACCEL_CTRL, ord('B'), boldid)])
self.SetAcceleratorTable(self.accel_tbl)
self.Show(True)
def OnAbout(self,e):
dlg = wx.MessageDialog(self, "A simple text editor", "About Simple Editor", wx.OK)
dlg.ShowModal()
dlg.Destroy()
def OnExit(self,e):
if self.control.IsModified:
dlg = wx.MessageDialog(self, "If you quit now all your work will be erased. Do you still want to quit?", "Are You Sure?", wx.YES_NO | wx.ICON_QUESTION)
a = dlg.ShowModal()
if a == wx.ID_YES:
self.Close(True)
else:
self.OnSave(self, True)
def OnOpen(self,e):
"""
Open a File
"""
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
newTab = tab(self.nb, name=self.filename, file=f.read(), aNewTab=True)
self.nb.AddPage(newTab, "%s" %(self.filename))
f.close()
self.SetTitle("Simple Editor - %s" %(self.filename))
dlg.Destroy()
def OnSave(self,e, exit=False):
"""
Save a file
"""
#if self.newTab.control.IsEmpty():
#dlg = wx.
self.dirname = ''
dlg = wx.FileDialog(self, "Where do you want to save this file?", self.dirname, "", "*.*", wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'w')
a = str(self.control.GetValue())
f.write(a)
f.close()
dlg.Destroy()
if exit != False:
self.Close(True)
self.SetTitle("Simple Editor - %s" %(self.filename))
app = wx.App(False)
frame = MainWindow(None, "Simple Editor")
app.MainLoop()