8
社区成员




传奇开心果使用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()