`
purpen
  • 浏览: 786093 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

wxPython写的简易编辑器(原创)

阅读更多
看了两天wxPython文档,作为练习简单做了一个简易的文本编辑器。有什么错误用法,希望一二,多谢!

呵呵,实现的功能是很简单的.(新建/打开/保存/另存/剪切/复制/粘贴/全选/删除^_^就这么多,算做simpleEditor beta1.0版吧。以后慢慢补充。)

python 代码:

# simpleEditor.py
 
  1. #!/usr/bin/env python  
  2. #coding=utf-8  
  3.   
  4. import wx  
  5. from wx.lib.wordwrap import wordwrap  
  6. import wx.richtext as rt  
  7. import ossys  
  8.   
  9. ID_NEW   =  wx.NewId()  
  10. ID_OPEN  =  wx.NewId()  
  11. ID_SAVE  =  wx.NewId()  
  12. ID_SAVEAS = wx.NewId()  
  13. ID_EXIT  =  wx.NewId()  
  14. ID_ABOUT =  wx.NewId()  
  15. ID_CUT   =  wx.NewId()  
  16. ID_COPY  =  wx.NewId()  
  17. ID_PASTE =  wx.NewId()  
  18. ID_SELECT = wx.NewId()  
  19. ID_CLEAR =  wx.NewId()  
  20.   
  21. wildcard = "Python source (*.py)|*.py|"    
  22.   
  23. class SimpleEditor(wx.Frame):  
  24.       
  25.     cur_file = ''  # current operate file  
  26.       
  27.     def __init__(self, parent, id, title):  
  28.         wx.Frame.__init__(self, parent, id, title, size=(1024, 768))  
  29.           
  30.         #------------create text area--------------  
  31.         edit_area = wx.TextCtrl(self, -1, style=wx.TE_MULTILINE)  
  32.         self.text = edit_area  
  33.         wx.CallAfter(self.text.SetFocus)  
  34.           
  35.         #------------create status bar-------------  
  36.         self.CreateStatusBar()  
  37.         self.SetStatusText('Here display operate status.')  
  38.         #------------create menubar----------------  
  39.         menubar = wx.MenuBar(wx.MB_DOCKABLE)  
  40.         #------------create menus------------------  
  41.         file = wx.Menu()  
  42.         edit = wx.Menu()  
  43.         help = wx.Menu()  
  44.           
  45.         file.Append(ID_NEW, '&New\tCtrl+N', 'Create a new File')  
  46.         file.Append(ID_OPEN, '&Open\tCtrl+O', 'Open a File')  
  47.         file.Append(ID_SAVE, '&Save\tCtrl+S', 'Save a File')  
  48.         file.Append(ID_SAVEAS, '&Save As\tShift+Ctrl+S', 'Save as a File')  
  49.         file.AppendSeparator()  
  50.         file.Append(ID_EXIT, 'E&xit\tCtrl+W', 'Exit Current window')  
  51.           
  52.         edit.Append(ID_CUT, '&Cut\tCtrl+X', 'Cut select text')  
  53.         edit.Append(ID_COPY, '&Copy\tCtrl+C', 'Copy select text')  
  54.         edit.Append(ID_PASTE, '&Paste\tCtrl+V', 'Paste clipboard text')  
  55.         edit.AppendSeparator()  
  56.         edit.Append(ID_SELECT, '&Select All\tCtrl+A', 'Select all')  
  57.         edit.Append(ID_CLEAR, '&Clear\tCtrl+D', 'delete select text')  
  58.   
  59.         help.Append(ID_ABOUT, '&About Simple Editor', 'Simple Editor Message')  
  60.         menubar.Append(file, '&File')  
  61.         menubar.Append(edit, '&Edit')  
  62.         menubar.Append(help, '&Help')  
  63.         self.Bind(wx.EVT_MENU, self.OnOpen, id=ID_OPEN)  
  64.         self.Bind(wx.EVT_MENU, self.OnSave, id=ID_SAVE)  
  65.         self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS)  
  66.         self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)  
  67.         self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT)  
  68.         self.Bind(wx.EVT_MENU, self.OnCut, id=ID_CUT)  
  69.         self.Bind(wx.EVT_MENU, self.OnCopy, id=ID_COPY)  
  70.         self.Bind(wx.EVT_MENU, self.OnSelectAll, id=ID_SELECT)  
  71.         self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)  
  72.         self.Bind(wx.EVT_MENU, self.OnPaste, id=ID_PASTE)  
  73.           
  74.         # set the menu bar  
  75.         self.SetMenuBar(menubar)     
  76.         #-------------------create tools bar-----------------------  
  77.         toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER  
  78.                                  | wx.TB_FLAT | wx.TB_TEXT)  
  79.         toolbar.AddLabelTool(ID_NEW, '', wx.Bitmap('icons/new.gif'))  
  80.         toolbar.AddLabelTool(ID_OPEN, '', wx.Bitmap('icons/open.gif'))  
  81.         toolbar.AddLabelTool(ID_SAVE, '', wx.Bitmap('icons/save.gif'))  
  82.         
  83.     #-------------------File menu event-------------------------  
  84.     def OnOpen(self, event):  
  85.         dlg = wx.FileDialog(  
  86.             self, message="Choose a file", defaultDir=os.getcwd(),  
  87.             defaultFile="", wildcard="All files (*.*)|*.*",  
  88.             style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR  
  89.             )  
  90.         if dlg.ShowModal() == wx.ID_OK:  
  91.             #paths = dlg.GetPaths()  
  92.             #print 'You selected files is %s:' % (paths)  
  93.             path = dlg.GetPath()  
  94.             if path: # open a file  
  95.                 self.cur_file = path  
  96.                 #self.text.SetValue(open(path,'r').read())  
  97.                 self.text.LoadFile(path, rt.RICHTEXT_TYPE_TEXT)  
  98.         dlg.Destroy()  
  99.           
  100.     def OnSave(self, event):  
  101.         cur_content = self.text.GetValue()  
  102.         if not self.cur_file:  
  103.             # if current file is new  
  104.             dlg = wx.FileDialog(  
  105.                 self, message="Save file as ...", defaultDir=os.getcwd(),  
  106.                 defaultFile="", wildcard="*.*", style=wx.SAVE  
  107.                 )  
  108.             if dlg.ShowModal() == wx.ID_OK:  
  109.                 self.cur_file = dlg.GetPath()  
  110.             elsepass         
  111.         if self.cur_file:  
  112.             fp = open(self.cur_file, 'w')  
  113.             fp.write(cur_content)  
  114.             fp.close()  
  115.           
  116.     def OnSaveAs(self, event):  
  117.         dlg = wx.FileDialog(  
  118.             self, "Choose a filename", wildcard="All files (*.*)|*.*",  
  119.             style=wx.SAVE)  
  120.         if dlg.ShowModal() == wx.ID_OK:  
  121.             path = dlg.GetPath()  
  122.             if path:  
  123.                 #self.text.SaveFile(path)  
  124.                 self.cur_file = path  
  125.                 fp = open(path, 'w')  
  126.                 fp.write(self.text.GetValue())  
  127.                 fp.close()  
  128.         dlg.Destroy()  
  129.               
  130.     def OnExit(self, event):  
  131.         self.Close(True)  
  132.     #-------------------Edit menu event---------------------------  
  133.     def OnCut(self, event):  
  134.         # Cut select text  
  135.         self.text.Cut()  
  136.           
  137.     def OnCopy(self, event):  
  138.         # copy select text  
  139.         self.text.Copy()  
  140.           
  141.     def OnPaste(self, event):  
  142.         # paste clipboard text  
  143.         self.text.Paste()  
  144.                 
  145.     def OnSelectAll(self, event):  
  146.         # select all text  
  147.         self.text.SelectAll()  
  148.           
  149.     def OnClear(self, event):  
  150.         # delete select text  
  151.         # self.text.Clear() is delete all text  
  152.         start,end = self.text.GetSelection()  
  153.         self.text.Remove(start, end)     
  154.           
  155.     #-------------------Help menu event---------------------------  
  156.     def OnAbout(self, event):  
  157.         # First we create and fill the info object  
  158.         info = wx.AboutDialogInfo()  
  159.         info.Name = "Simple Editor"  
  160.         info.Version = "beta 1.0"  
  161.         info.Copyright = "(C) 2008 Programmers and Coders Everywhere"  
  162.         # copy from wxPython demo  
  163.         info.Description = wordwrap(  
  164.             "A \"hello world\" program is a software program that prints out "  
  165.             "\"Hello world!\" on a display device. It is used in many introductory "  
  166.             "tutorials for teaching a programming language."  
  167.               
  168.             "\n\nSuch a program is typically one of the simplest programs possible "  
  169.             "in a computer language. A \"hello world\" program can be a useful "  
  170.             "sanity test to make sure that a language's compiler, development "  
  171.             "environment, and run-time environment are correctly installed.",  
  172.             350, wx.ClientDC(self))  
  173.         info.WebSite = ("http://www.bobo.com.cn", "BOBO VIDEO")  
  174.         info.Developers = ["Purpen"]  
  175.         # Then we call wx.AboutBox giving it that info object  
  176.         wx.AboutBox(info)  
  177.       
  178. class RunApp(wx.App):  
  179.     def OnInit(self):  
  180.         win = SimpleEditor(None, 1, 'SimpleEditor')  
  181.         self.SetTopWindow(win)  
  182.         win.Show(True)  
  183.         return True  # can't forget  
  184.           
  185. if __name__ == '__main__':  
  186.     RunApp().MainLoop()  

下载可以直接在命令行下运行(当然,环境一定要支持)。
分享到:
评论
3 楼 最佳蜗牛 2013-03-04  
最佳蜗牛 写道
是不是缺少了一个toolbar.Realize()?

第81行下面
2 楼 最佳蜗牛 2013-03-04  
是不是缺少了一个toolbar.Realize()?
1 楼 最佳蜗牛 2013-03-04  
2.7.3版本里不能显示工具栏的图标

相关推荐

Global site tag (gtag.js) - Google Analytics