<textarea cols="50" rows="15" name="code" class="python:collapse:showcolumns"># -*- coding: UTF-8 -*- import wx; import os; text = "This directory contains the sample programs that were printed in the wxPython In Action book. For details and explainations of each,please see the coresponding chapter in the book." class EditContainer(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1,"text edit",size=(500,500),style=wx.DEFAULT_FRAME_STYLE); self.Bind(wx.EVT_KEY_DOWN,self.frameKeyDown,self); self.textColor = wx.BLACK; self.textFamily = wx.EmptyString; self.textSize = 9 self.textBold = wx.NORMAL; self.textUnder = False; self.textStyle = wx.NORMAL; self.CreateStatusBar(); #menu menu1 = wx.Menu(); menu1.AppendSeparator(); newMenu = menu1.Append(-1,"&nNEW FILE","create new file"); saveMenu = menu1.Append(-1,"&sSAVE FILE","SAVE FILE"); self.Bind(wx.EVT_MENU,self.newFile,newMenu); self.Bind(wx.EVT_MENU,self.openFileDialog,saveMenu); menuBar = wx.MenuBar() menuBar.Append(menu1,"&mfile"); self.SetMenuBar(menuBar); #style fontList = wx.FontEnumerator().GetFacenames(); self.fontCB = wx.ComboBox(self,-1,"SWISS",wx.DefaultPosition,(150,30),fontList,wx.CB_DROPDOWN); self.Bind(wx.EVT_COMBOBOX,self.formatFamily,self.fontCB); sizeList = ["8","9","10","12","14","16","18","20","22","24","26","28","30","32","34","36","40","44","48","50"]; self.sizeCB = wx.ComboBox(self,-1,"9",wx.DefaultPosition,(50,30),sizeList,wx.CB_DROPDOWN); self.Bind(wx.EVT_COMBOBOX,self.formatSize,self.sizeCB); styleSizer = wx.BoxSizer(wx.HORIZONTAL); styleSizer.Add((5,5),0) styleSizer.Add(self.fontCB); styleSizer.Add((5,5),0) styleSizer.Add(self.sizeCB); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("F", self.openFontDialog)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("B", self.formatBold)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("I", self.formatITALIC)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("U", self.formatUnder)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("C", self.openColorDialog)); styleSizer.Add((5,5),0) #text edit self.richText = wx.TextCtrl(self,-1,text,style=wx.MULTIPLE|wx.TE_RICH2); mainSizer = wx.BoxSizer(wx.VERTICAL); mainSizer.Add((5,5),0); mainSizer.Add(styleSizer,0,wx.EXPAND|wx.ALL); mainSizer.Add(self.richText,1,wx.EXPAND|wx.ALL,5); self.SetSizer(mainSizer); self.SetFocus(); def frameKeyDown(self,event): code = event.GetKeyCode(); #print code; if event.ControlDown() and code == 78: #ctrl + n self.newFile(); elif event.ControlDown() and code == 83: #ctrl + s self.openFileDialog(); else: event.Skip(); def formatFamily(self,event): selectRange = self.getTextSelect(); self.textFamily = self.fontCB.GetValue(); #print self.textFamily; self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr()) def formatSize(self,event): selectRange = self.getTextSelect(); self.textSize = int(self.sizeCB.GetValue()); self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr()) def createButton(self,label="",fun=""): btn = wx.Button(self,wx.NewId(),label,size=(25,25)); btn.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.BOLD)) self.Bind(wx.EVT_BUTTON,fun,btn); return btn; def openColorDialog(self,event): selectRange = self.getTextSelect(); dialog = wx.ColourDialog(None); dialog.GetColourData().SetChooseFull(True); if dialog.ShowModal() == wx.ID_OK: data = dialog.GetColourData(); self.formatColor(data,selectRange); dialog.Destroy(); def formatUnder(self): selectRange = self.getTextSelect(); self.textUnder = True; self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr()) def formatColor(self,col,selectRange): self.richText.SetSelection(selectRange[0],selectRange[1]); #selectRange = self.getTextSelect(); self.textColor = col.GetColour(); self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr()) def formatITALIC(self,event): selectRange = self.getTextSelect(); self.textStyle = wx.ITALIC; self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr()) def formatBold(self,event): selectRange = self.getTextSelect(); self.textBold = wx.BOLD; self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr()) def getTextSelect(self): return self.richText.GetSelection(); def getFontTextAttr(self): return wx.TextAttr(self.textColor,"",self.getFontStyle()); def getFontStyle(self): return wx.Font(self.textSize,wx.SWISS,self.textStyle,self.textBold,self.textUnder,self.textFamily,wx.FONTENCODING_DEFAULT) def createFontTextAtrr(self,s,f,t,b,u): tempFont = wx.Font(s,f,t,b,u) return wx.TextAttr(self.textColor,"",tempFont); def openFontDialog(self,event): dialog = wx.FontDialog(None,wx.FontData()) if dialog.ShowModal() == wx.ID_OK: data = dialog.GetFontData() font = data.GetChosenFont() self.textFamily = font.GetFaceName(); self.textSize = font.GetPointSize(); self.textColor = data.GetColour(); self.textBold = font.GetWeight(); self.textStyle = font.GetStyle(); self.textUnder = font.GetUnderlined(); self.richText.SetStyle(0,self.richText.GetLastPosition(),self.getFontTextAttr()) dialog.Destroy(); def newFile(self='',event=''): self.richText.SetValue(""); def openFileDialog(self = '',event=''): woldcard = "All files(*.txt)|*.txt"; dialog = wx.FileDialog(None,"save file",os.getcwd(),"newText.txt",woldcard,wx.SAVE); if dialog.ShowModal() == wx.ID_OK: self.saveFile(dialog.GetPath()); def saveFile(self,path): f = file(path,"w+"); f.write(self.richText.GetValue()); f.close(); self.SetStatusText("save complete!!!!!!!!!!!!!") if __name__ == "__main__": app = wx.PySimpleApp() edit = EditContainer() edit.SetIcon(wx.Icon("../assets/text.ico",wx.BITMAP_TYPE_ICO)); edit.Show() app.MainLoop(); </textarea>