python — TextCtrl sample

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.