python2.7练习 写一个简单的文本编辑器
2015-05-06 10:16
477 查看
# -*- coding: utf-8 -*- import wx, os, sys class MenuBarWindow(wx.Frame): def __init__(self, parent, title): # A "-1" in the size parameter instructs wxWidgets to use the default size. # In this case, we select 200px width and the default height. wx.Frame.__init__(self, parent, title=title, size=wx.DefaultSize) self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE) # A StatusBar in the bottom of the window self.CreateStatusBar() # Setting up the menu. filemenu = wx.Menu() # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets. menuAbout = filemenu.Append(wx.ID_ABOUT, "About", " Information about this program") menuOpen = filemenu.Append(wx.ID_OPEN, "Open", "Open a file to editor") menuExit = filemenu.Append(wx.ID_EXIT, "Exit", " Terminate the program") menuSave = filemenu.Append(wx.ID_SAVE, "Save", "Save the file") # Creating the menubar. menuBar = wx.MenuBar() menuBar.Append(filemenu, "File") # Adding the "filemenu" to the MenuBar self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content. # Set Events self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen) self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout) self.Bind(wx.EVT_MENU, self.OnExit, menuExit) self.Bind(wx.EVT_MENU, self.OnSave, menuSave) self.Show(True) def OnExit(self, event): print 'exit' self.Close(True) def OnAbout(self, event): print 'about' dlg = wx.MessageDialog(self, "A small text editor", "About Sample Editor", wx.OK) dlg.ShowModal() # Show it dlg.Destroy() # finally destroy it when finished. def OnOpen(self, event): print 'open a file' filepath = '' try: dlg = wx.FileDialog(self, "Choose a file", '', '', '*.*', wx.OPEN) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() filepath = os.path.join(dirname, filename) f = open(filepath, 'r') self.control.SetValue(f.read()) f.close() dlg.Destroy() except IOError: type, val, traceback = sys.exc_info() dlg = wx.MessageDialog(self, str(val), "", wx.OK) dlg.ShowModal() # Show it dlg.Destroy() # finally destroy it when finished. def OnSave(self, event): print "save the file" dlg = wx.FileDialog(self, "save the file", '', '', '*.*', wx.SAVE) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() # get Text content from TextCtrl data = self.control.GetValue() dataEncode = data.encode('utf-8') f = open(os.path.join(dirname, filename), 'w') f.write(dataEncode) f.close() dlg.Destroy() if __name__ == '__main__': app = wx.App(False) frame = MenuBarWindow(None, "My Editor") app.MainLoop()
相关文章推荐
- 【Python】Learn Python the hard way, ex35 通过一个简单游戏,练习循环和分支语句
- [python]一个简单的听写练习工具
- [QT小项目练习]TextEditor 一个简单的文本编辑器
- python使用wxPython创建一个简单的文本编辑器。
- 一个简单的python练习
- python通过Tkinter库实现的一个简单的文本编辑器代码
- python2.7_1.14_编写一个简单的回显客户端/服务器应用
- 开始学习Python,设置环境和编译一个简单的程序
- python 实现一个简单的 WebSocket 服务器
- Python写的一个简单的备份程序
- python实现的一个简单web应用
- python小练习9(这些简单的题目你都会吗)
- Python练习(1):递归和动态规划的简单应用
- python实现一个简单的爬虫
- 一个简单的python程序实例(通讯录)
- 如何用Python编写一个简单的爬虫
- 一个简单的python socket编程
- [Python]一个简单的数据库类
- 用Python实现一个简单的线程池
- Cocoa练习01:一个简单的Todo list程序