python -- TextCtrl sample

<textarea cols="50" rows="15" name="code" class="python:collapse:showcolumns"># -*- coding: UTF-8 -*- import wx; import os; 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.Bind(wx.EVT_COMBOBOX,self.formatFamily,self.fontCB); 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.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(&quot;F&quot;, self.openFontDialog)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;B&quot;, self.formatBold)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;I&quot;, self.formatITALIC)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;U&quot;, self.formatUnder)); styleSizer.Add((5,5),0) styleSizer.Add(self.createButton(&quot;C&quot;, 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=&quot;&quot;,fun=&quot;&quot;): 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,&quot;&quot;,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,&quot;&quot;,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(&quot;&quot;); 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): 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.Show() app.MainLoop(); </textarea>

你可能感兴趣的:(python -- TextCtrl sample)