<textarea cols="50" rows="15" name="code" class="python:collapse:showcolumns"># -*- coding: UTF-8 -*- import wx; import os; import cStringIO; import wx.html; import base64 import webbrowser; import wx.richtext as rt; 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.fontCB.Bind(wx.EVT_COMBOBOX,self.formatFamily); 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.sizeCB.Bind(wx.EVT_COMBOBOX,self.formatSize); 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,"set font family")); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("B", self.formatBold,"set font bold")); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("I", self.formatITALIC,"set font style")); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("U", self.formatUnder,"set font underline")); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton("C", self.openColorDialog,"set font color")); styleSizer.Add((5,5),0); styleSizer.Add(self.createButton("M", self.openImageFile,"load local file")) styleSizer.Add((5,5),0); styleSizer.Add(self.createButton("R", self.openSetURL,"set char url")) styleSizer.Add((5,5),0); #text edit self.richText = rt.RichTextCtrl(self,-1,text,style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER); self.Bind(wx.EVT_TEXT_URL,self.openrichTextURL); 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 openrichTextURL(self,event): webbrowser.open(event.GetString()); def openSetURL(self,event): dialog = wx.TextEntryDialog(None,"entry char url","set url",""); if(dialog.ShowModal() == wx.ID_OK): self.formatURL(dialog.GetValue()); dialog.Destroy(); def formatURL(self,url): selectRange = self.getTextSelect(); self.textUnder = True; self.textColor = wx.RED; self.richText.SetStyle(selectRange,self.getFontTextAttr(url)) def formatFamily(self,event): selectRange = self.getTextSelect(); self.textFamily = self.fontCB.GetValue(); #print self.textFamily; self.richText.SetStyle(selectRange,self.getFontTextAttr()) def formatSize(self,event): selectRange = self.getTextSelect(); self.textSize = int(self.sizeCB.GetValue()); self.richText.SetStyle(selectRange,self.getFontTextAttr()) def createButton(self,label="",fun="",tip=''): btn = wx.Button(self,wx.NewId(),label,size=(25,25)); btn.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.BOLD)) btn.SetName(tip); if(tip!=''): btn.SetToolTipString(tip); self.SetStatusText(tip); btn.Bind(wx.EVT_BUTTON,fun); btn.Bind(wx.EVT_ENTER_WINDOW,self.controlOver) btn.Bind(wx.EVT_LEAVE_WINDOW,self.controlOut) return btn; def controlOver(self,event): btn = event.GetEventObject() self.SetStatusText(btn.GetName()); event.Skip(); def controlOut(self,event): self.SetStatusText(""); event.Skip(); def formatUnder(self): selectRange = self.getTextSelect(); self.textUnder = True; self.richText.SetStyle(selectRange,self.getFontTextAttr()) def formatColor(self,col): #self.richText.SetSelectionRange(selectRange); selectRange = self.getTextSelect(); self.textColor = col.GetColour(); self.richText.SetStyle(selectRange,self.getFontTextAttr()) def formatITALIC(self,event): selectRange = self.getTextSelect(); self.textStyle = wx.ITALIC; self.richText.SetStyle(selectRange,self.getFontTextAttr()) def formatBold(self,event): selectRange = self.getTextSelect(); self.textBold = wx.BOLD; self.richText.SetStyle(selectRange,self.getFontTextAttr()) def getTextSelect(self): return self.richText.GetSelection(); def getFontTextAttr(self,url=''): attr = rt.TextAttrEx(); attr.SetFontFaceName(self.textFamily); attr.SetFontStyle(self.textStyle); attr.SetFontSize(self.textSize) attr.SetFontUnderlined(self.textUnder); attr.SetFontWeight(self.textBold); attr.SetTextColour(self.textColor) if(url!=''): attr.SetURL(url) return attr; 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 openNetworkImage(self,event): dialog = wx.TextEntryDialog(None,"entry network file","image file",""); if(dialog.ShowModal() == wx.ID_OK): self.insertNetWorkImage(dialog.GetValue()); dialog.Destroy(); 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 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); dialog.Destroy(); def newFile(self='',event=''): self.richText.SetValue(""); def openImageFile(self,event): woldcart = "All files|*.*|jpg|*.jpg|jpeg|*.jpeg|png|*.png"; dialog = wx.FileDialog(None,"insert image",os.getcwd(),"",woldcart,wx.OPEN) if(dialog.ShowModal()==wx.ID_OK): self.insertImage(dialog.GetPath()); def insertImage(self,path=''): self.richText.WriteImageFile(path,wx.BITMAP_TYPE_ANY); def insertNetWorkImage(self,path=""): self.richText.WriteImageFile(path,wx.BITMAP_TYPE_ANY); 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): #print self.richText.GetValue(); 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.Center(); edit.Show() app.MainLoop(); </textarea>