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正则表达式之作业计算器
Mar 18 Python
利用Python破解斗地主残局详解
Jun 30 Python
Python爬取附近餐馆信息代码示例
Dec 09 Python
Python实现的拟合二元一次函数功能示例【基于scipy模块】
May 15 Python
python3.6使用pymysql连接Mysql数据库
May 25 Python
Python实现E-Mail收集插件实例教程
Feb 06 Python
Python实现Restful API的例子
Aug 31 Python
python sqlite的Row对象操作示例
Sep 11 Python
python实现的config文件读写功能示例
Sep 24 Python
Python 使用Opencv实现目标检测与识别的示例代码
Sep 08 Python
Django实现文章详情页面跳转代码实例
Sep 16 Python
anaconda升级sklearn版本的实现方法
Feb 22 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
第三节 定义一个类 [3]
2006/10/09 PHP
深入浅出php socket编程
2015/05/13 PHP
php基于单例模式封装mysql类完整实例
2016/10/18 PHP
PHP编译configure时常见错误的总结
2017/08/17 PHP
JS图片无缝、平滑滚动代码
2014/03/11 Javascript
JavaScript中的slice()方法使用详解
2015/06/06 Javascript
一不小心就做错的JS闭包面试题
2015/11/25 Javascript
GitHub上一些实用的JavaScript的文件压缩解压缩库推荐
2016/03/13 Javascript
基于BootStrap Metronic开发框架经验小结【五】Bootstrap File Input文件上传插件的用法详解
2016/05/12 Javascript
jquery submit()不能提交表单的解决方法
2017/04/24 jQuery
JavaScript面向对象精要(上部)
2017/09/12 Javascript
swiper 自动图片无限轮播实现代码
2018/05/21 Javascript
微信小程序scroll-view实现字幕滚动
2018/07/14 Javascript
vue工程全局设置ajax的等待动效的方法
2019/02/22 Javascript
react使用CSS实现react动画功能示例
2020/05/18 Javascript
如何在Vue.JS中使用图标组件
2020/08/04 Javascript
基于vue hash模式微信分享#号的解决
2020/09/07 Javascript
[00:47]DOTA2荣耀之路6:天火,天火!
2018/05/30 DOTA
python用字典统计单词或汉字词个数示例
2014/04/22 Python
在RedHat系Linux上部署Python的Celery框架的教程
2015/04/07 Python
Python计算三角函数之asin()方法的使用
2015/05/15 Python
python提取页面内url列表的方法
2015/05/25 Python
Python从ZabbixAPI获取信息及实现Zabbix-API 监控的方法
2018/09/17 Python
python实现列表中最大最小值输出的示例
2019/07/09 Python
python实现BP神经网络回归预测模型
2019/08/09 Python
pytorch 固定部分参数训练的方法
2019/08/17 Python
Python threading的使用方法解析
2019/08/28 Python
Pycharm+django2.2+python3.6+MySQL实现简单的考试报名系统
2019/09/05 Python
Python django框架输入汉字,数字,字符生成二维码实现详解
2019/09/24 Python
BASIC HOUSE官方旗舰店:韩国著名的服装品牌
2018/09/27 全球购物
瑞士隐形眼镜和护理产品网上商店:Linsenklick
2019/10/21 全球购物
美国购买隐形眼镜网站:Lenses For Less
2020/07/05 全球购物
应急管理培训方案
2014/06/12 职场文书
单位未婚证明范本
2014/11/25 职场文书
企业愿景口号
2015/12/25 职场文书
laravel添加角色和模糊搜索功能的实现代码
2021/06/22 PHP