本文实例讲述了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程序设计有所帮助。
标签:
Python,文本编辑器
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Python实现的文本编辑器功能示例”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2025年10月28日
2025年10月28日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
