基于Python怎样写一个简单的文本编辑器
Admin 2022-09-22 群英技术资讯 1049 次浏览
今天就跟大家聊聊有关“基于Python怎样写一个简单的文本编辑器”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“基于Python怎样写一个简单的文本编辑器”文章能对大家有帮助。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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了Python密码学XOR流程及乘法密码教程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
有时候看到一篇好的文章,想去保存下来,传统方式一般是收藏书签、复制粘贴到文档或者直接复制链接保存,但这也太麻烦了。本文将用Python语言实现将网上的文章转存为PDF文档,保存电脑上慢慢看
通常需要对前端传递过来的参数进行校验,校验的方式有多种,本文主要介绍了Python中rapidjson参数校验实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
深度学习已经成为机器学习中最受欢迎和发展最快的领域。深度学习的常见应用包括语音识别、图像识别、自然语言处理、推荐系统等等。本文将通过一些示例代码,带你详细了解深入学习
这篇文章主要介绍了Python中tkinter的用户登录管理的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008