700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python实现文本编辑器功能实例详解

Python实现文本编辑器功能实例详解

时间:2024-07-13 06:38:35

相关推荐

Python实现文本编辑器功能实例详解

后端开发|Python教程

Python,编辑器,功能

后端开发-Python教程wxpython实现的文本编辑器 效果如下:

商标源码,vscode搜索头文件路径,直接进ubuntu,tomcat内存溢出了,大蒜炸爬虫,日本免费php,湖北seo网络推广哪家好,制作表白网站源码html,招聘网站源码模板lzw

简单asp网页源码,vscode插入图片教程,ubuntu分配硬盘,tomcat 解压版配置,爬虫机场,php拖动排序,上饶seo推广公司价格,网贷中介平台网站源码,smarty网页模板下载lzw

主要功能:

签到积分系统源码6,ubuntu系统安装程序,xcode爬虫怎么写,php黑,淮阴seo推广lzw

1.编辑保存文本,打开修改文本

2.常用快捷键,复制,粘贴,全选等

3.支持撤销功能

4.支持弹出式菜单

代码如下:

#encoding=utf-8import wximport osclass 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(,u剪切 Ctrl + X) menu2.Append(,u复制 Ctrl + C) menu2.Append(,u粘贴 Ctrl + V ) menu2.Append(,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=) self.Bind(wx.EVT_MENU, self.OnCopy, id=) self.Bind(wx.EVT_MENU, self.OnPaste, id=) self.Bind(wx.EVT_MENU, self.OnSelect, id=) self.Bind(wx.EVT_SIZE, self.OnResize) new=wx.Image(./icons/new.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() open=wx.Image(./icons/open.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() exit=wx.Image(./icons/exit.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() save=wx.Image(./icons/save.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() saveall=wx.Image(./icons/saveall.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() back=wx.Image(./icons/back.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() go=wx.Image(./icons/go.jpg,wx.BITMAP_TYPE_PNG).ConvertToBitmap() clear=wx.Image(./icons/clear.jpg,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-1self.text.SetValue(self.content[self.count]) except IndexError:self.count=0 def OnTGo(self,event): try:self.count=self.count+1self.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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。