Python实现的文本编辑器功能示例

Python实现的文本编辑器功能示例,第1张

概述本文实例讲述了Python实现的文本编辑器功能。分享给大家供大家参考,具体如下:

本文实例讲述了Python实现的文本编辑器功能。分享给大家供大家参考,具体如下:

wxpython实现的文本编辑器 效果如下:

主要功能:

1.编辑保存文本,打开修改文本
2.常用快捷键,复制,粘贴,全选等
3.支持撤销功能
4.支持d出式菜单

代码如下:

#enCoding=utf-8import wximport osclass MyFrame(wx.Frame):  def __init__(self):    self.file=''    self.content=[]    self.count=0    self.wIDth=700    self.height=500    wx.Frame.__init__(self,None,-1,u'记事本',size=(self.wIDth,self.height))    self.panel=wx.Panel(self,-1)    menubar=wx.Menubar()    menu1=wx.Menu()    menubar.Append(menu1,u'文件')    menu1.Append(1001,u'打开')    menu1.Append(1002,u'保存')    menu1.Append(1003,u'另存为')    menu1.Append(1004,u'退出')    menu2=wx.Menu()    menubar.Append(menu2,u'编辑')    menu2.Append(2001,u'撤销')    menu2.Append(2002,u'清空')    menu2.Append(2003,u'剪切 Ctrl + X')    menu2.Append(2004,u'复制 Ctrl + C')    menu2.Append(2005,u'粘贴 Ctrl + V ')    menu2.Append(2006,u'全选 Ctrl + A',)    menu=wx.Menu()    ctrla=menu.Append(-1,"\tCtrl-A")    ctrlc=menu.Append(-1,"\tCtrl-C")    ctrlx=menu.Append(-1,"\tCtrl-X")    ctrlv=menu.Append(-1,"\tCtrl-V")    ctrls=menu.Append(-1,"\tCtrl-S")    menubar.Append(menu,'')    self.SetMenubar(menubar)    self.Bind(wx.EVT_MENU,self.OnSelect,ctrla)    self.Bind(wx.EVT_MENU,self.Oncopy,ctrlc)    self.Bind(wx.EVT_MENU,self.OnCut,self.OnPaste,ctrlv)    self.Bind(wx.EVT_MENU,self.OnTSave,ctrls)    self.Bind(wx.EVT_MENU,self.Onopen,ID=1001)    self.Bind(wx.EVT_MENU,self.OnSave,ID=1002)    self.Bind(wx.EVT_MENU,self.OnSaveAll,ID=1003)    self.Bind(wx.EVT_MENU,self.OnExit,ID=1004)    self.Bind(wx.EVT_MENU,self.OnBack,ID=2001)    self.Bind(wx.EVT_MENU,self.OnClear,ID=2002)    self.Bind(wx.EVT_MENU,ID=2003)    self.Bind(wx.EVT_MENU,ID=2004)    self.Bind(wx.EVT_MENU,ID=2005)    self.Bind(wx.EVT_MENU,ID=2006)    self.Bind(wx.EVT_SIZE,self.OnResize)    new=wx.Image('./icons/new.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    open=wx.Image('./icons/open.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    exit=wx.Image('./icons/exit.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    save=wx.Image('./icons/save.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    saveall=wx.Image('./icons/saveall.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    back=wx.Image('./icons/back.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    go=wx.Image('./icons/go.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    clear=wx.Image('./icons/clear.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap()    toolbar=self.Createtoolbar(wx.TB_HORIZONTAL|wx.TB_TEXT)    toolbar.AddSimpletool(100,new,'New')    toolbar.AddSimpletool(200,open,'Open')    toolbar.AddSimpletool(300,exit,'Exit')    toolbar.AddSimpletool(400,save,'Save')    toolbar.AddSimpletool(500,saveall,'Save All')    toolbar.AddSimpletool(600,back,'Back')    toolbar.AddSimpletool(700,go,'Go')    toolbar.AddSimpletool(800,clear,'Clear')    toolbar.Realize()    self.Bind(wx.EVT_TOol,self.Ontopen,ID=200)    self.Bind(wx.EVT_TOol,self.OnTExit,ID=300)    self.Bind(wx.EVT_TOol,ID=400)    self.Bind(wx.EVT_TOol,self.OnTBack,ID=600)    self.Bind(wx.EVT_TOol,self.OnTGo,ID=700)    self.Bind(wx.EVT_TOol,self.OnTClear,ID=800)    self.text=wx.TextCtrl(self.panel,pos=(2,2),size=(self.wIDth-10,self.height-50),style=wx.HSCRolL|wx.TE_MulTIliNE)    self.popupmenu = wx.Menu()#创建一个菜单    for text in "Cut copy Paste SelectAll".split():#填充菜单      item = self.popupmenu.Append(-1,text)      self.Bind(wx.EVT_MENU,self.OnPopupItemSelected,item)      self.panel.Bind(wx.EVT_CONTEXT_MENU,self.OnShowPopup)#绑定一个显示菜单事件  def OnShowPopup(self,event):#d出显示    pos = event.Getposition()    pos = self.panel.ScreenToClIEnt(pos)    self.panel.PopupMenu(self.popupmenu,pos)  def OnPopupItemSelected(self,event):    item = self.popupmenu.FindItemByID(event.GetID())    text = item.GetText()    if text=='Cut':      self.OnCut(event)    elif text=='copy':      self.Oncopy(event)    elif text=='Paste':      self.OnPaste(event)    elif text=='SelectAll':      self.OnSelect(event)  def Onopen(self,event):    filterfile=" All files (*.*) |*.*"    opendialog=wx.fileDialog(self,u"选择文件",os.getcwd(),"",filterfile,wx.OPEN)    if opendialog.ShowModal()==wx.ID_OK:      self.file=opendialog.GetPath()      f=open(self.file)      self.text.write(f.read())      f.close()    opendialog.Destroy()  def Ontopen(self,event):    filterfile="All files (*.*) |*.*"    opendialog=wx.fileDialog(self,wx.OPEN)    if opendialog.ShowModal()==wx.ID_OK:      self.file=opendialog.GetPath()      f=open(self.file)      self.text.write(f.read())      f.close()      self.content.append(self.text.GetValue())    opendialog.Destroy()  def OnSave(self,u'保存文件',wx.SAVE)    if opendialog.ShowModal()==wx.ID_OK:      self.file=opendialog.GetPath()      self.text.Savefile(self.file)  def OnTSave(self,event):    if self.file == '':      filterfile="All files (*.*) |*.*"      opendialog=wx.fileDialog(self,wx.SAVE)      if opendialog.ShowModal()==wx.ID_OK:        self.file=opendialog.GetPath()        self.text.Savefile(self.file)        self.content.append(self.text.GetValue())        self.count=self.count+1    else:      self.text.Savefile(self.file)      self.content.append(self.text.GetValue())      self.count=self.count+1  def OnSaveAll(self,event):      pass  def OnExit(self,event):    self.Close()  def OnTExit(self,event):    self.Close()  def OnBack(self,event):    self.text.Undo()  def OnTBack(self,event):    try:      self.count=self.count-1      self.text.SetValue(self.content[self.count])    except IndexError:      self.count=0  def OnTGo(self,event):    try:      self.count=self.count+1      self.text.SetValue(self.content[self.count])    except IndexError:      self.count=len(self.content)-1  def OnClear(self,event):    self.text.Clear()  def OnTClear(self,event):    self.text.Clear()  def OnCut(self,event):    self.text.Cut()  def Oncopy(self,event):    self.text.copy()  def OnPaste(self,event):    self.text.Paste()  def OnSelect(self,event):    self.text.SelectAll()  def OnResize(self,event):    newsize=self.GetSize()    wIDth=newsize.GetWIDth()-10    height=newsize.GetHeight()-50    self.text.SetSize((wIDth,height))    self.text.Refresh()if @R_403_4267@=='__main__':  app=wx.App()  myFrame=MyFrame()  myFrame.Show()  app.MainLoop()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串 *** 作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录 *** 作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Python实现的文本编辑器功能示例全部内容,希望文章能够帮你解决Python实现的文本编辑器功能示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1202020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存