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

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

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

主要功能:

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 Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

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

(0)

相关推荐

  • python使用wxpython开发简单记事本的方法

    本文实例讲述了python使用wxpython开发简单记事本的方法.分享给大家供大家参考.具体分析如下: wxPython是Python编程语言的一个GUI工具箱.他使得Python程序员能够轻松的创建具有健壮.功能强大的图形用户界面的程序.它是Python语言对流行的wxWidgets跨平台GUI工具库的绑定.而wxWidgets是用C++语言写成的. 和Python语言与wxWidgetsGUI工具库一样,wxPython是开源软件.这意味着任何人都可以免费地使用它并且可以查看和修改它的源代

  • python写的一个文本编辑器

    复制代码 代码如下: #!/usr/bin/env python#-*- coding: utf-8 -*-#=============================================================================#     FileName:#         Desc:#       Author: ToughGuy#      Version: 0.0.1#   LastChange: 2013-02-20 14:52:11#      H

  • 基于wxpython开发的简单gui计算器实例

    本文实例讲述了基于wxpython开发的简单gui计算器.分享给大家供大家参考.具体如下: # wxCalc1 a simple GUI calculator using wxPython # created with the Boa Constructor which generates all the GUI components # all I had to do is add some code for each button click event # Boa free from: h

  • 好用的Python编辑器WingIDE的使用经验总结

    WingIDE的使用 好的工具可以让你做事时,事半功倍!这一点在写代码的过程中尤为明显,使用Pyhton写程序有一年多了!各类编辑器IDE也使用了不少,如Pycharm,sublime,Qtcreator等等,最近开始使用WingIDE,发现使用起来非常顺手,而且支持目前所有主流操作系统!这篇博文用来记录总结,在安装和使用WingIDE过程中的一些有用的经验,技巧! 官网下载链接:http://wingware.com/downloads WingIDE的安装 在windows平台下的安装 1:

  • python使用wxPython打开并播放wav文件的方法

    本文实例讲述了python使用wxPython打开并播放wav文件的方法.分享给大家供大家参考.具体实现方法如下: ''' wx_lib_filebrowsebutton_sound.py select a sound file and play it wx.lib.filebrowsebutton.FileBrowseButton(parent, labelText, fileMask) (combines wx.TextCtrl and wxFileDialog widgets) wx.So

  • Python的Flask站点中集成xhEditor文本编辑器的教程

    xhEditor简介 xhEditor是一个基于jQuery开发的简单迷你并且高效的可视化HTML编辑器,基于网络访问并且兼容IE 6.0+, Firefox 3.0+, Opera 9.6+, Chrome 1.0+, Safari 3.22+. xhEditor曾经是我比较喜欢的编辑器,也是率先支持拖拽上传的编辑器之一.xhEditor在当年是优秀的编辑器,功能足够强大,使用体验也相当好,拖拽上传是我最喜欢的功能,只可惜已经停止开发了.xhEditor最后的稳定版本是1.1.14,至今已超过

  • Python的Flask框架中集成CKeditor富文本编辑器的教程

    CKeditor是目前最优秀的可见即可得网页编辑器之一,它采用JavaScript编写.具备功能强大.配置容易.跨浏览器.支持多种编程语言.开源等特点.它非常流行,互联网上很容易找到相关技术文档,国内许多WEB项目和大型网站均采用了CKeditor. 下载CKeditor 访问CKeditor官方网站,进入下载页面,选择Standard Package(一般情况下功能足够用了),然后点击Download CKEditor按钮下载ZIP格式的安装文件.如果你想尝试更多的功能,可以选择下载Full

  • Python中使用wxPython开发的一个简易笔记本程序实例

    一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets的Python封装和Python模块的方式提供给用户的. 二.安装 参考官方网站:http://www.wxpython.org/download.php 三.DEMO 本demo是一个简单的记事本软件,可以打开文件,修改并保存. import wx app = wx.App() win = wx.

  • python基于Tkinter库实现简单文本编辑器实例

    本文实例讲述了python基于Tkinter库实现简单文本编辑器的方法.分享给大家供大家参考.具体实现方法如下: ## {{{ http://code.activestate.com/recipes/578568/ (r1) from Tkinter import * from tkSimpleDialog import askstring from tkFileDialog import asksaveasfilename from tkMessageBox import askokcance

  • 使用Python读写文本文件及编写简单的文本编辑器

    学习raw_input和argv是学习读取文件的前提,你可能不能完全理解这个练习,所以认真学习并检查.如果不认真的话,很容易删除一些有用的文件. 这个练习包含两个文件,一个是运行文件ex15.py,一个是ex15_sample.txt.第二个文件不是脚本文件,只包括一些文本,如下: This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. 我们要做的就是打开这

随机推荐