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


Posted in Python onJune 30, 2017

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

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

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

主要功能:

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

代码如下:

#encoding=utf-8
import wx
import os
class 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,ctrlc)
    self.Bind(wx.EVT_MENU, 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, self.OnCut, id=2003)
    self.Bind(wx.EVT_MENU, self.OnCopy, id=2004)
    self.Bind(wx.EVT_MENU, self.OnPaste, id=2005)
    self.Bind(wx.EVT_MENU, self.OnSelect, 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,self.OnTSave,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,-1,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):#弹出显示
    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,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()
      self.content.append(self.text.GetValue())
    opendialog.Destroy()
  def OnSave(self,event):
    filterFile="All files (*.*) |*.*"
    opendialog=wx.FileDialog(self,u'保存文件',os.getcwd(),"",filterFile,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,u'保存文件',os.getcwd(),"",filterFile,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 __name__=='__main__':
  app=wx.App()
  myFrame=MyFrame()
  myFrame.Show()
  app.MainLoop()

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

Python 相关文章推荐
用python实现批量重命名文件的代码
May 25 Python
Python实现简单HTML表格解析的方法
Jun 15 Python
Python批量修改文本文件内容的方法
Apr 29 Python
tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法
Jul 27 Python
Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项
Nov 30 Python
浅谈PYTHON 关于文件的操作
Mar 19 Python
Python3的高阶函数map,reduce,filter的示例详解
Jul 23 Python
浅谈Python3 numpy.ptp()最大值与最小值的差
Aug 24 Python
Python单例模式的四种创建方式实例解析
Mar 04 Python
使用python采集Excel表中某一格数据
May 14 Python
keras 自定义loss model.add_loss的使用详解
Jun 22 Python
golang中的空接口使用详解
Mar 30 Python
Python构建XML树结构的方法示例
Jun 30 #Python
基于python的Tkinter编写登陆注册界面
Jun 30 #Python
Python使用微信SDK实现的微信支付功能示例
Jun 30 #Python
python实现的二叉树定义与遍历算法实例
Jun 30 #Python
Python使用openpyxl读写excel文件的方法
Jun 30 #Python
python中关于for循环的碎碎念
Jun 30 #Python
Python实现的微信公众号群发图片与文本消息功能实例详解
Jun 30 #Python
You might like
完美解决dedecms中的[html][/html]和[code][/code]问题
2007/03/20 PHP
php防注入及开发安全详细解析
2013/08/09 PHP
PHP实现二维数组根据key进行排序的方法
2016/12/30 PHP
PHP chunk_split()函数讲解
2019/02/12 PHP
Javascript 圆角div的实现代码
2009/10/15 Javascript
将HTML的左右尖括号等转义成实体形式的两种实现方式
2014/05/04 Javascript
jquery实现显示已选用户
2014/07/21 Javascript
JavaScript之数组(Array)详解
2015/04/01 Javascript
javascript省市级联功能实现方法实例详解
2015/10/20 Javascript
js实现延时加载Flash的方法
2015/11/26 Javascript
jQuery+CSS3文字跑马灯特效的简单实现
2016/06/25 Javascript
JS跨域交互(jQuery+php)之jsonp使用心得
2016/07/01 Javascript
jquery根据td给相同tr下其他td赋值的实现方法
2016/10/05 Javascript
ES6新特性三: Generator(生成器)函数详解
2017/04/21 Javascript
解决jquery appaend元素中id绑定事件失效的问题
2017/09/12 jQuery
vue中使用cropperjs的方法
2018/03/01 Javascript
在Vue组件中获取全局的点击事件方法
2018/09/06 Javascript
jQuery - AJAX load() 实例用法详解
2019/08/27 jQuery
浅谈vue限制文本框输入数字的正确姿势
2019/09/02 Javascript
Python入门篇之函数
2014/10/20 Python
python开发利器之ulipad的使用实践
2017/03/16 Python
Python读取word文本操作详解
2018/01/22 Python
Python中正则表达式的用法总结
2019/02/22 Python
python交易记录链的实现过程详解
2019/07/03 Python
详解程序意外中断自动重启shell脚本(以Python为例)
2019/07/26 Python
Python读取ini配置文件传参的简单示例
2021/01/05 Python
基于Python的接口自动化unittest测试框架和ddt数据驱动详解
2021/01/27 Python
CSS3实现多背景模拟动态边框的效果
2016/11/08 HTML / CSS
德国汽车零件和汽车配件网上商店:kfzteile24
2018/11/14 全球购物
英国在线玫瑰专家:InterRose
2019/12/01 全球购物
软件缺陷的分类都有哪些
2014/08/22 面试题
农村党支部书记四风问题个人对照检查材料
2014/09/21 职场文书
2015新年寄语(一句话)
2014/12/08 职场文书
老人节主持词
2015/07/04 职场文书
教师节主题班会方案
2015/08/17 职场文书
社交电商模式的兴起:这些新的商机千万别错过
2019/07/26 职场文书