本文实例讲述了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实现的文本编辑器功能示例相关推荐

  1. python读取json配置文件_Python简单读取json文件功能示例

    本文实例讲述了Python简单读取json文件功能.分享给大家供大家参考,具体如下: read_json.json: { "rule":{ "namespace" ...

  2. python清理微信好友_Python实现清理微信僵尸粉功能示例【基于itchat模块】

    本文实例讲述了Python实现清理微信僵尸粉功能.分享给大家供大家参考,具体如下: 原理 通过Pyhton调用itchat模块登录网页版微信,给你所有好友发送特殊符号,对方收不到这个特殊符号,只要有人 ...

  3. python 邮件服务器地址_python实现的接收邮件功能示例【基于网易POP3服务器】

    本文实例讲述了python实现的接收邮件功能.分享给大家供大家参考,具体如下: 一 简介 本代码实现从网易POP3服务器接收邮件 二 代码 import poplib import re import ...

  4. python扫描端口脚本_Python实现的端口扫描功能示例

    本文实例讲述了Python实现的端口扫描功能.分享给大家供大家参考,具体如下: 一 代码 import sys import socket import multiprocessing def por ...

  5. python ip代理池_python实现ip代理池功能示例

    本文实例讲述了python实现ip代理池功能.分享给大家供大家参考,具体如下: 爬取的代理源为西刺代理. 用xpath解析页面 用telnet来验证ip是否可用 把有效的ip写入到本地txt中.当然也 ...

  6. python编写科学计算器_Python实现的科学计算器功能示例

    本文实例讲述了Python实现的科学计算器功能.分享给大家供大家参考,具体如下: import wx import re import math # begin wxGlade: extracode ...

  7. python标签整理 微信_Python实现清理微信僵尸粉功能示例【基于itchat模块】

    本文实例讲述了Python实现清理微信僵尸粉功能.分享给大家供大家参考,具体如下: 原理 通过Pyhton调用itchat模块登录网页版微信,给你所有好友发送特殊符号,对方收不到这个特殊符号,只要有人 ...

  8. python 拼音相似度_python 計算文本的相似度

    用Python計算文本的相似度 因為后期會需要用到這方面的知識,所以先提前准備准備:如何判斷網頁返回內容的相似度? 找到了幾個Python的方法和庫: 還有高大上的"TF-IDF方法&quo ...

  9. python json dumps 自定义_Python json.dumps()用法及代码示例

    JSON的完整形式是JavaScript Object Notation.这意味着将使用编程语言的文本组成的脚本(可执行)文件用于存储和传输数据. Python通过名为内置的软件包支持JSONjson ...

  10. python制作自动回复脚本_python itchat实现微信自动回复的示例代码

    今天在实验楼发现一个特别好玩的,Python 微信库itchat,可以实现自动回复等多种功能,好玩到根本停不下来啊,尤其是调戏调戏不懂计算机的,特别有成就感,哈哈!! 代码如下: #coding=ut ...

最新文章

  1. 每日一皮:听说学琵琶的都很文弱...
  2. ICE 迁移64位安装问题
  3. android MotionEvent中getX()和getRawX()的区别
  4. 一个不错的游戏 - flash webgame
  5. python基础知识下载_Python基础知识(一)
  6. macos安装盘第三方工具制作_简单制作OSXYosemite10.10正式版U盘USB启动安装盘方法教程(全新安装Mac系统)下载|异次元软件世界...
  7. SpringCloud 从菜鸟到大牛之六 消息和异步 MQ
  8. 1090 Highest Price in Supply Chain (25 分)(模拟建树,找树的深度)牛客网过,pat没过...
  9. ios中蓝牙自动连接出现硬件提示框的问题
  10. 成功申请MVP,晒晒来自微软的奖品
  11. CTF必备密码编码大全
  12. 地下水数值模拟软件有哪些?GMS、Visual modflow、FEFLOW哪个更好用呢?
  13. RDDs, Spark Memory, and Execution
  14. matlab gradient函数原理
  15. 「docker实战篇」python的docker- 抖音视频抓取(中)(25)
  16. 卡塔尔世界杯——你认为今年谁会是冠军?
  17. Day02-线性代数-矩阵(DataWhale)
  18. Shader混合模式--正片叠底、滤色、叠加
  19. 计算机专业法语词汇,法语词汇学习:计算机及网络词汇(1)
  20. python实现微信自动发拜年信息和回复消息

热门文章

  1. 蚂蚁金服Java后台实习生春招面试总结
  2. c语言开发宏程序,一文搞懂宏程序的编程基础,快速入门秘笈
  3. MYSQL实现排序分组取第一条sql
  4. 如何能从自我怀疑中走出来?
  5. 电脑安装系统时提示“找不到硬盘”如何解决
  6. c语言按一个按钮弹出窗口,实现点击按钮,弹出输入框的内容
  7. 直线端点画垂线lisp_AutoCAD中利用AutoLISP开发小程序,实现快速画直线对称中心线...
  8. java gps 纠偏_【实测可用】GPS纠偏算法-Java版
  9. CSP赛前集训 【DD头子张京华】
  10. android手机控制电视,类Siri语音控制技术 Android手机也能控制电视