import wx
import os
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200, -1))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.dirname = ""
self.filename = ""
self.content = ""
self.Show()
# Attribute
statusBar = self.CreateStatusBar()
fileMenu = wx.Menu()
optMenu = wx.Menu()
openf = fileMenu.Append(wx.ID_OPEN, "Open..", "Open a file")
exit = fileMenu.Append(wx.ID_EXIT, "Exit", "Quit the program")
about = optMenu.Append(wx.ID_ABOUT, "About", "About this software")
# Assign menus to menubar and activate menu bar
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, "File")
menuBar.Append(optMenu, "Option")
self.SetMenuBar(menuBar)
# Bind EVT to the menu choices
self.Bind(wx.EVT_MENU, self.OnOpen, openf)
self.Bind(wx.EVT_MENU, self.OnExit, exit)
self.Bind(wx.EVT_MENU, self.OnAbout, about)
# Setup BoxSizer
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.buttons = []
for i in range(0,6):
self.buttons.append(wx.Button(self, -1, "Button &"+str(i)))
self.sizer2.Add(self.button[i], 1, wx.EXPAND)
# Use some sizers to see layout options
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.control, 1, wx.EXPAND)
self.sizer.Add(self.sizer2, 0, wx.EXPAND)
# Layout sizers
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
#self.Show()
def OnOpen(self, event):
self.content = ""
opendlg = wx.FileDialog(self, "Open a file..", self.dirname, "", "*.*", wx.OPEN)
if opendlg.ShowModal() == wx.ID_OK:
self.dirname = opendlg.GetDirectory()
self.filename = opendlg.GetFilename()
f = open(os.path.join(self.dirname, self.filename), "r")
self.content = f.read()
self.control.SetValue(self.content) # set TextCtrl value
f.close()
opendlg.Destroy()
def OnExit(self, event):
if self.control.GetValue() != self.content:
self.control.SaveFile(os.path.join(self.dirname, self.filename))
self.Destroy()
def OnAbout(self, event):
aboutdlg = wx.MessageDialog(self, "This is a free dummy editor.", "About editor", wx.OK)
aboutdlg.ShowModal()
aboutdlg.Destroy()
app = wx.App(True)
frame = MyFrame(None, title="Editor")
app.MainLoop()