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 相关文章推荐
[原创]pip和pygal的安装实例教程
Dec 07 Python
Python实现动态图解析、合成与倒放
Jan 18 Python
tf.truncated_normal与tf.random_normal的详细用法
Mar 05 Python
使用python将请求的requests headers参数格式化方法
Jan 02 Python
使用Django简单编写一个XSS平台的方法步骤
Mar 25 Python
解决Django加载静态资源失败的问题
Jul 28 Python
详解Python 字符串相似性的几种度量方法
Aug 29 Python
使用 Python 清理收藏夹里已失效的网站
Dec 03 Python
Python关键字及可变参数*args,**kw原理解析
Apr 04 Python
Python通过两个dataframe用for循环求笛卡尔积
Apr 29 Python
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
Dec 09 Python
Python机器学习之基于Pytorch实现猫狗分类
Jun 08 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
PHP伪静态页面函数附使用方法
2008/06/20 PHP
AMFPHP php远程调用(RPC, Remote Procedure Call)工具 快速入门教程
2010/05/10 PHP
php设置session值和cookies的学习示例
2014/03/21 PHP
javascript demo 基本技巧
2009/12/18 Javascript
JavaScript EasyPager 分页函数
2011/05/25 Javascript
jQuery EasyUI API 中文文档 - Tree树使用介绍
2011/11/19 Javascript
javascript中setTimeout的问题解决方法
2014/05/08 Javascript
jquery操作checkbox示例分享
2014/07/21 Javascript
jQuery打印图片pdf、txt示例代码
2014/07/22 Javascript
jQuery中has()方法用法实例
2015/01/06 Javascript
js实现禁止中文输入的方法
2015/01/14 Javascript
jQuery 局部div刷新和全局刷新方法总结
2016/10/05 Javascript
jquery移除了live()、die(),新版事件绑定on()、off()的方法
2016/10/26 Javascript
微信小程序 wx.request(接口调用方式)详解及实例
2016/11/23 Javascript
jQuery插件FusionCharts绘制的2D双柱状图效果示例【附demo源码】
2017/05/13 jQuery
vue组件详解之使用slot分发内容
2018/04/09 Javascript
python连接mysql调用存储过程示例
2014/03/05 Python
利用Python中的mock库对Python代码进行模拟测试
2015/04/16 Python
Windows安装Python、pip、easy_install的方法
2017/03/05 Python
Python:Scrapy框架中Item Pipeline组件使用详解
2017/12/27 Python
Python3实现的画图及加载图片动画效果示例
2018/01/19 Python
Python实现图片转字符画的代码实例
2019/02/22 Python
python并发编程多进程之守护进程原理解析
2019/08/20 Python
python使用opencv在Windows下调用摄像头实现解析
2019/11/26 Python
详细分析Python可变对象和不可变对象
2020/07/09 Python
美国在线鲜花速递:ProFlowers
2017/01/05 全球购物
台湾演唱会订票网站:StubHub台湾
2019/06/11 全球购物
大学生大二自我鉴定
2013/10/28 职场文书
银行出纳岗位职责
2013/11/25 职场文书
前处理组长岗位职责
2014/03/01 职场文书
大学军训感言400字
2014/03/11 职场文书
写给孩子的新学期寄语
2015/02/27 职场文书
《鸡兔同笼》教学反思
2016/02/19 职场文书
java设计模式--七大原则详解
2021/07/21 Java/Android
排查MySQL生产环境索引没有效果
2022/04/11 MySQL
二维码条形码生成的JavaScript脚本库
2022/07/07 Javascript