python -- TextCtrl sample

python -- TextCtrl sample - 宋欢的博客 - 博客频道 - CSDN.NET http://blog.csdn.net/songhuanren/article/details/4497556

 

  1 # -*- coding: UTF-8 -*-
  2 import wx;
  3 import os;
  4 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."
  5 class EditContainer(wx.Frame):
  6     def __init__(self):
  7         wx.Frame.__init__(self,None,-1,"text edit",size=(500,500),style=wx.DEFAULT_FRAME_STYLE);
  8         self.Bind(wx.EVT_KEY_DOWN,self.frameKeyDown,self);
  9         self.textColor  = wx.BLACK;
 10         self.textFamily = wx.EmptyString;
 11         self.textSize   = 9
 12         self.textBold   = wx.NORMAL;
 13         self.textUnder  = False;
 14         self.textStyle  = wx.NORMAL;
 15         self.CreateStatusBar();
 16         #menu
 17         menu1 = wx.Menu();
 18         menu1.AppendSeparator();
 19         newMenu = menu1.Append(-1,"&nNEW FILE","create new file");
 20         saveMenu = menu1.Append(-1,"&sSAVE FILE","SAVE FILE");
 21         self.Bind(wx.EVT_MENU,self.newFile,newMenu);
 22         self.Bind(wx.EVT_MENU,self.openFileDialog,saveMenu);
 23         menuBar = wx.MenuBar()
 24         menuBar.Append(menu1,"&mfile");
 25         self.SetMenuBar(menuBar);
 26         #style
 27         fontList = wx.FontEnumerator().GetFacenames();
 28         self.fontCB = wx.ComboBox(self,-1,"SWISS",wx.DefaultPosition,(150,30),fontList,wx.CB_DROPDOWN);
 29         self.Bind(wx.EVT_COMBOBOX,self.formatFamily,self.fontCB);
 30         sizeList = ["8","9","10","12","14","16","18","20","22","24","26","28","30","32","34","36","40","44","48","50"];
 31         self.sizeCB = wx.ComboBox(self,-1,"9",wx.DefaultPosition,(50,30),sizeList,wx.CB_DROPDOWN);
 32         self.Bind(wx.EVT_COMBOBOX,self.formatSize,self.sizeCB);
 33         styleSizer = wx.BoxSizer(wx.HORIZONTAL);
 34         styleSizer.Add((5,5),0)
 35         styleSizer.Add(self.fontCB);
 36         styleSizer.Add((5,5),0)
 37         styleSizer.Add(self.sizeCB);
 38         styleSizer.Add((5,5),0)
 39         styleSizer.Add(self.createButton("F", self.openFontDialog));
 40         styleSizer.Add((5,5),0)
 41         styleSizer.Add(self.createButton("B", self.formatBold));
 42         styleSizer.Add((5,5),0)
 43         styleSizer.Add(self.createButton("I", self.formatITALIC));
 44         styleSizer.Add((5,5),0)
 45         styleSizer.Add(self.createButton("U", self.formatUnder));
 46         styleSizer.Add((5,5),0)
 47         styleSizer.Add(self.createButton("C", self.openColorDialog));
 48         styleSizer.Add((5,5),0)
 49         #text edit
 50         self.richText = wx.TextCtrl(self,-1,text,style=wx.MULTIPLE|wx.TE_RICH2);
 51         mainSizer = wx.BoxSizer(wx.VERTICAL);
 52         mainSizer.Add((5,5),0);
 53         mainSizer.Add(styleSizer,0,wx.EXPAND|wx.ALL);
 54         mainSizer.Add(self.richText,1,wx.EXPAND|wx.ALL,5);
 55 
 56         self.SetSizer(mainSizer);
 57         self.SetFocus();
 58     def frameKeyDown(self,event):
 59         code = event.GetKeyCode();
 60         #print code;
 61         if event.ControlDown() and code == 78:
 62             #ctrl + n
 63             self.newFile();
 64         elif event.ControlDown() and code == 83:
 65             #ctrl + s
 66             self.openFileDialog();
 67         else:
 68             event.Skip();
 69     def formatFamily(self,event):
 70         selectRange = self.getTextSelect();
 71         self.textFamily = self.fontCB.GetValue();
 72         #print self.textFamily;
 73         self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr())
 74     def formatSize(self,event):
 75         selectRange = self.getTextSelect();
 76         self.textSize = int(self.sizeCB.GetValue());
 77         self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr())
 78     def createButton(self,label="",fun=""):
 79         btn = wx.Button(self,wx.NewId(),label,size=(25,25));
 80         btn.SetFont(wx.Font(12,wx.SWISS,wx.NORMAL,wx.BOLD))
 81         self.Bind(wx.EVT_BUTTON,fun,btn);
 82         return btn;
 83     def openColorDialog(self,event):
 84         selectRange = self.getTextSelect();
 85         dialog = wx.ColourDialog(None);
 86         dialog.GetColourData().SetChooseFull(True);
 87         if dialog.ShowModal() == wx.ID_OK:
 88             data = dialog.GetColourData();
 89             self.formatColor(data,selectRange);
 90         dialog.Destroy();
 91     def formatUnder(self):
 92         selectRange = self.getTextSelect();
 93         self.textUnder = True;
 94         self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr())
 95     def formatColor(self,col,selectRange):
 96         self.richText.SetSelection(selectRange[0],selectRange[1]);
 97         #selectRange = self.getTextSelect();
 98         self.textColor = col.GetColour();
 99         self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr())
100     def formatITALIC(self,event):
101         selectRange = self.getTextSelect();
102         self.textStyle = wx.ITALIC;
103         self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr())
104     def formatBold(self,event):
105         selectRange = self.getTextSelect();
106         self.textBold = wx.BOLD;
107         self.richText.SetStyle(selectRange[0],selectRange[1],self.getFontTextAttr())
108     def getTextSelect(self):
109         return self.richText.GetSelection();
110     def getFontTextAttr(self):
111         return wx.TextAttr(self.textColor,"",self.getFontStyle());
112     def getFontStyle(self):
113         return wx.Font(self.textSize,wx.SWISS,self.textStyle,self.textBold,self.textUnder,self.textFamily,wx.FONTENCODING_DEFAULT)
114     def createFontTextAtrr(self,s,f,t,b,u):
115         tempFont = wx.Font(s,f,t,b,u)
116         return wx.TextAttr(self.textColor,"",tempFont);
117     def openFontDialog(self,event):
118         dialog = wx.FontDialog(None,wx.FontData())
119         if dialog.ShowModal() == wx.ID_OK:
120             data = dialog.GetFontData()
121             font = data.GetChosenFont()
122             self.textFamily = font.GetFaceName();
123             self.textSize   = font.GetPointSize();
124             self.textColor  = data.GetColour();
125             self.textBold   = font.GetWeight();
126             self.textStyle  = font.GetStyle();
127             self.textUnder  = font.GetUnderlined();
128             self.richText.SetStyle(0,self.richText.GetLastPosition(),self.getFontTextAttr())
129         dialog.Destroy();
130     def newFile(self='',event=''):
131         self.richText.SetValue("");
132     def openFileDialog(self = '',event=''):
133         woldcard = "All files(*.txt)|*.txt";
134         dialog = wx.FileDialog(None,"save file",os.getcwd(),"newText.txt",woldcard,wx.SAVE);
135         if dialog.ShowModal() == wx.ID_OK:
136             self.saveFile(dialog.GetPath());
137     def saveFile(self,path):
138         f = file(path,"w+");
139         f.write(self.richText.GetValue());
140         f.close();
141         self.SetStatusText("save complete!!!!!!!!!!!!!")
142 
143 if __name__ == "__main__":
144     app = wx.PySimpleApp()
145     edit = EditContainer()
146     edit.SetIcon(wx.Icon("../assets/text.ico",wx.BITMAP_TYPE_ICO));
147     edit.Show()
148     app.MainLoop();

 

 

posted @ 2014-07-06 11:37  追风的蜗牛  阅读(722)  评论(0)    收藏  举报