服务器之家

服务器之家 > 正文

Python实现的文本编辑器功能示例

时间:2020-11-22 21:43     来源/作者:yueguanghaidao

本文实例讲述了Python实现的文本编辑器功能。分享给大家供大家参考,具体如下:

wxpython实现的文本编辑器 效果如下:

Python实现的文本编辑器功能示例

主要功能:

1.编辑保存文本,打开修改文本
2.常用快捷键,复制,粘贴,全选等
3.支持撤销功能
4.支持弹出式菜单

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#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程序设计有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部