wxpythno -- RichTextCtrl (sample)

<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 = &quot;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.&quot; class EditContainer(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1,&quot;text edit&quot;,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,&quot;&amp;nNEW FILE&quot;,&quot;create new file&quot;); saveMenu = menu1.Append(-1,&quot;&amp;sSAVE FILE&quot;,&quot;SAVE FILE&quot;); self.Bind(wx.EVT_MENU,self.newFile,newMenu); self.Bind(wx.EVT_MENU,self.openFileDialog,saveMenu); menuBar = wx.MenuBar() menuBar.Append(menu1,&quot;&amp;mfile&quot;); self.SetMenuBar(menuBar); #style fontList = wx.FontEnumerator().GetFacenames(); self.fontCB = wx.ComboBox(self,-1,&quot;SWISS&quot;,wx.DefaultPosition,(150,30),fontList,wx.CB_DROPDOWN); self.fontCB.Bind(wx.EVT_COMBOBOX,self.formatFamily); sizeList = [&quot;8&quot;,&quot;9&quot;,&quot;10&quot;,&quot;12&quot;,&quot;14&quot;,&quot;16&quot;,&quot;18&quot;,&quot;20&quot;,&quot;22&quot;,&quot;24&quot;,&quot;26&quot;,&quot;28&quot;,&quot;30&quot;,&quot;32&quot;,&quot;34&quot;,&quot;36&quot;,&quot;40&quot;,&quot;44&quot;,&quot;48&quot;,&quot;50&quot;]; self.sizeCB = wx.ComboBox(self,-1,&quot;9&quot;,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(&quot;F&quot;, self.openFontDialog,&quot;set font family&quot;)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;B&quot;, self.formatBold,&quot;set font bold&quot;)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;I&quot;, self.formatITALIC,&quot;set font style&quot;)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;U&quot;, self.formatUnder,&quot;set font underline&quot;)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;C&quot;, self.openColorDialog,&quot;set font color&quot;)); styleSizer.Add((5,5),0); styleSizer.Add(self.createButton(&quot;M&quot;, self.openImageFile,&quot;load local file&quot;)) styleSizer.Add((5,5),0); styleSizer.Add(self.createButton(&quot;R&quot;, self.openSetURL,&quot;set char url&quot;)) 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,&quot;entry char url&quot;,&quot;set url&quot;,&quot;&quot;); 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=&quot;&quot;,fun=&quot;&quot;,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(&quot;&quot;); 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,&quot;&quot;,tempFont); def openNetworkImage(self,event): dialog = wx.TextEntryDialog(None,&quot;entry network file&quot;,&quot;image file&quot;,&quot;&quot;); 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(&quot;&quot;); def openImageFile(self,event): woldcart = &quot;All files|*.*|jpg|*.jpg|jpeg|*.jpeg|png|*.png&quot;; dialog = wx.FileDialog(None,&quot;insert image&quot;,os.getcwd(),&quot;&quot;,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=&quot;&quot;): self.richText.WriteImageFile(path,wx.BITMAP_TYPE_ANY); def openFileDialog(self = '',event=''): woldcard = &quot;All files(*.txt)|*.txt&quot;; dialog = wx.FileDialog(None,&quot;save file&quot;,os.getcwd(),&quot;newText.txt&quot;,woldcard,wx.SAVE); if dialog.ShowModal() == wx.ID_OK: self.saveFile(dialog.GetPath()); def saveFile(self,path): #print self.richText.GetValue(); f = file(path,&quot;w+&quot;); f.write(self.richText.GetValue()); f.close(); self.SetStatusText(&quot;save complete!!!!!!!!!!!!!&quot;) if __name__ == &quot;__main__&quot;: app = wx.PySimpleApp() edit = EditContainer() edit.SetIcon(wx.Icon(&quot;../assets/text.ico&quot;,wx.BITMAP_TYPE_ICO)); edit.Center(); edit.Show() app.MainLoop(); </textarea>

你可能感兴趣的:(wxpythno -- RichTextCtrl (sample))