Python實(shí)現(xiàn)的文本編輯器功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)的文本編輯器功能。分享給大家供大家參考,具體如下:
wxpython實(shí)現(xiàn)的文本編輯器 效果如下:

主要功能:
1.編輯保存文本,打開(kāi)修改文本
2.常用快捷鍵,復(fù)制,粘貼,全選等
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'打開(kāi)')
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'復(fù)制 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()#創(chuàng)建一個(gè)菜單
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)#綁定一個(gè)顯示菜單事件
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()
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python的Flask站點(diǎn)中集成xhEditor文本編輯器的教程
- Python的Flask框架中集成CKeditor富文本編輯器的教程
- 使用Python讀寫(xiě)文本文件及編寫(xiě)簡(jiǎn)單的文本編輯器
- python基于Tkinter庫(kù)實(shí)現(xiàn)簡(jiǎn)單文本編輯器實(shí)例
- python寫(xiě)的一個(gè)文本編輯器
- 好用的Python編輯器WingIDE的使用經(jīng)驗(yàn)總結(jié)
- 基于wxpython開(kāi)發(fā)的簡(jiǎn)單gui計(jì)算器實(shí)例
- python使用wxpython開(kāi)發(fā)簡(jiǎn)單記事本的方法
- python使用wxPython打開(kāi)并播放wav文件的方法
- Python中使用wxPython開(kāi)發(fā)的一個(gè)簡(jiǎn)易筆記本程序?qū)嵗?/a>
相關(guān)文章
PyCharm2020.1.2社區(qū)版安裝,配置及使用教程詳解(Windows)
這篇文章主要介紹了PyCharm2020.1.2社區(qū)版安裝,配置及使用教程(Windows),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Python wxauto 庫(kù)解鎖微信自動(dòng)化的無(wú)限可能(示例代碼)
wxauto庫(kù)是基于Python的一個(gè)自動(dòng)化工具,它主要用于操作和自動(dòng)化WxPython應(yīng)用程序,這篇文章主要介紹了Python wxauto 庫(kù)解鎖微信自動(dòng)化的無(wú)限可能,需要的朋友可以參考下2024-07-07
python2 與 python3 實(shí)現(xiàn)共存的方法
這篇文章主要介紹了python2 與 python3 實(shí)現(xiàn)共存的方法,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
Python 爬蟲(chóng)圖片簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Python 爬蟲(chóng)圖片簡(jiǎn)單實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06
Python讀取文件比open快十倍的庫(kù)fileinput

