python2.7练习 写一个简单的文本编辑器

本文档展示了如何使用Python2.7和wxWidgets库创建一个简单的文本编辑器。编辑器包含文件菜单,支持打开、保存、退出操作,并在主窗口中显示文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- 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()
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值