使用wxpython的子模块wx.lib.agw.aui 写一个记事本示例代码,运行和调试通过

传奇开心果编程
Python领域优质创作者
2024-09-10 10:45:56

img

传奇开心果使用IDE运行和调试通过

import wx
import wx.lib.agw.aui as aui

class NotePadFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="记事本", size=(800, 600))

        # 创建菜单栏
        menu_bar = wx.MenuBar()
        
        # 文件菜单
        file_menu = wx.Menu()
        new_item = file_menu.Append(wx.ID_NEW, "新建", "新建文件")
        open_item = file_menu.Append(wx.ID_OPEN, "打开", "打开文件")
        save_item = file_menu.Append(wx.ID_SAVE, "保存", "保存文件")
        menu_bar.Append(file_menu, "文件")

        # 编辑菜单
        edit_menu = wx.Menu()
        undo_item = edit_menu.Append(wx.ID_UNDO, "撤销", "撤销上一步操作")
        redo_item = edit_menu.Append(wx.ID_REDO, "重做", "重做上一步操作")
        edit_menu.AppendSeparator()
        cut_item = edit_menu.Append(wx.ID_CUT, "剪切", "剪切选中的文本")
        copy_item = edit_menu.Append(wx.ID_COPY, "复制", "复制选中的文本")
        paste_item = edit_menu.Append(wx.ID_PASTE, "粘贴", "粘贴文本")
        menu_bar.Append(edit_menu, "编辑")
        
        self.SetMenuBar(menu_bar)

        # 工具栏创建
        self.toolbar = self.CreateToolBar()
        self.toolbar.AddTool(wx.ID_NEW, "新建", wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR))
        self.toolbar.AddTool(wx.ID_OPEN, "打开", wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR))
        self.toolbar.AddTool(wx.ID_SAVE, "保存", wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE, wx.ART_TOOLBAR))
        self.toolbar.AddSeparator()
        self.toolbar.AddTool(wx.ID_UNDO, "撤销", wx.ArtProvider.GetBitmap(wx.ART_UNDO, wx.ART_TOOLBAR))
        self.toolbar.AddTool(wx.ID_REDO, "重做", wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_TOOLBAR))
        self.toolbar.AddSeparator()
        self.toolbar.AddTool(wx.ID_CUT, "剪切", wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR))
        self.toolbar.AddTool(wx.ID_COPY, "复制", wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR))
        self.toolbar.AddTool(wx.ID_PASTE, "粘贴", wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR))
        self.toolbar.Realize()

        # 绑定菜单和工具栏事件
        self.Bind(wx.EVT_MENU, self.on_new, new_item)
        self.Bind(wx.EVT_MENU, self.on_open, open_item)
        self.Bind(wx.EVT_MENU, self.on_save, save_item)
        self.Bind(wx.EVT_MENU, self.on_undo, undo_item)
        self.Bind(wx.EVT_MENU, self.on_redo, redo_item)
        self.Bind(wx.EVT_MENU, self.on_cut, cut_item)
        self.Bind(wx.EVT_MENU, self.on_copy, copy_item)
        self.Bind(wx.EVT_MENU, self.on_paste, paste_item)
        
        self.Bind(wx.EVT_TOOL, self.on_new, id=wx.ID_NEW)
        self.Bind(wx.EVT_TOOL, self.on_open, id=wx.ID_OPEN)
        self.Bind(wx.EVT_TOOL, self.on_save, id=wx.ID_SAVE)
        self.Bind(wx.EVT_TOOL, self.on_undo, id=wx.ID_UNDO)
        self.Bind(wx.EVT_TOOL, self.on_redo, id=wx.ID_REDO)
        self.Bind(wx.EVT_TOOL, self.on_cut, id=wx.ID_CUT)
        self.Bind(wx.EVT_TOOL, self.on_copy, id=wx.ID_COPY)
        self.Bind(wx.EVT_TOOL, self.on_paste, id=wx.ID_PASTE)

        # 使用AUI管理器创建一个选项卡管理
        self.mgr = aui.AuiManager(self)
        self.notebook = aui.AuiNotebook(self)
        self.mgr.AddPane(self.notebook, aui.AuiPaneInfo().CenterPane().PaneBorder(False))
        self.mgr.Update()

    def on_new(self, event):
        new_tab = wx.TextCtrl(self.notebook, style=wx.TE_MULTILINE)
        self.notebook.AddPage(new_tab, "新建文件")

    def on_open(self, event):
        with wx.FileDialog(self, "打开文件", wildcard="Text files (*.txt)|*.txt",
                           style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return  # 用户取消
            
            path = fileDialog.GetPath()
            try:
                with open(path, 'r', encoding='utf-8') as file:
                    content = file.read()
                    new_tab = wx.TextCtrl(self.notebook, style=wx.TE_MULTILINE)
                    new_tab.SetValue(content)
                    self.notebook.AddPage(new_tab, path.split('/')[-1])
            except IOError:
                wx.LogError(f"无法打开文件 '{path}'。")

    def on_save(self, event):
        current_page = self.notebook.GetCurrentPage()
        if current_page:
            with wx.FileDialog(self, "保存文件", wildcard="Text files (*.txt)|*.txt",
                               style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
                if fileDialog.ShowModal() == wx.ID_CANCEL:
                    return  # 用户取消
                
                path = fileDialog.GetPath()
                try:
                    with open(path, 'w', encoding='utf-8') as file:
                        content = current_page.GetValue()
                        file.write(content)
                except IOError:
                    wx.LogError(f"无法保存文件 '{path}'。")

    def on_undo(self, event):
        current_page = self.notebook.GetCurrentPage()
        if current_page and current_page.CanUndo():
            current_page.Undo()

    def on_redo(self, event):
        current_page = self.notebook.GetCurrentPage()
        if current_page and current_page.CanRedo():
            current_page.Redo()

    def on_cut(self, event):
        current_page = self.notebook.GetCurrentPage()
        if current_page:
            current_page.Cut()

    def on_copy(self, event):
        current_page = self.notebook.GetCurrentPage()
        if current_page:
            current_page.Copy()

    def on_paste(self, event):
        current_page = self.notebook.GetCurrentPage()
        if current_page:
            current_page.Paste()

class MyApp(wx.App):
    def OnInit(self):
        frame = NotePadFrame()
        frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()
...全文
553 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

8

社区成员

发帖
与我相关
我的任务
社区描述
软件开发那些事
软件工程需求分析团队开发 个人社区 甘肃省·酒泉市
社区管理员
  • 传奇开心果编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧