wxpython记事本
时间: 2023-10-18 12:04:46 浏览: 159
下面是一个简单的wxPython记事本的示例代码:
```python
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(400, 400))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar()
filemenu = wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open a file")
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save file")
menuAbout = filemenu.Append(wx.ID_ABOUT, "&About", "Information about this program")
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate the program")
menuBar = wx.MenuBar()
menuBar.Append(filemenu, "&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Show(True)
def OnAbout(self,e):
dlg = wx.MessageDialog(self, " A simple text editor", "About Sample Editor", wx.OK)
dlg.ShowModal()
dlg.Destroy()
def OnExit(self,e):
self.Close(True)
def OnOpen(self,e):
""" Open a file"""
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()
def OnSave(self,e):
""" Save a file"""
dlg = wx.FileDialog(self, "Save file", self.dirname, "", "*.*", wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'w')
f.write(self.control.GetValue())
f.close()
dlg.Destroy()
app = wx.App(False)
frame = MyFrame(None, 'Simple Editor')
app.MainLoop()
```
这个记事本具有基本的文件打开、保存、退出和关于等功能。你可以根据需要进行修改和扩展。
阅读全文