当前位置:编程学习 > python >>

python实现的文本编辑器

wxpython实现的文本编辑器

效果如下:

 \
 

主要功能:

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

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

3.支持撤销功能

 

 

 

代码如下:

#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'剪切')
        menu2.Append(2004,u'复制')
        menu2.Append(2005,u'粘贴')
        menu2.Append(2006,u'全选',)

www.zzzyk.com
        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()


        易做图=self.CreateToolBar(wx.TB_HORIZONTAL|wx.TB_TEXT)
        易做图.AddSimpleTool(100,new,'New')
        易做图.AddSimpleTool(200,open,'Open')
        易做图.AddSimpleTool(300,exit,'Exit')
        易做图.AddSimpleTool(400,save,'Save')
        易做图.AddSimpleTool(500,saveall,'Save All')
        易做图.AddSimpleTool(600,back,'Back')
        易做图.AddSimpleTool(700,go,'Go')
        易做图.AddSimpleTool(800,clear,'Clear')
        易做图.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)


    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())
 &nb

补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,