2.6 Adding File Menu Functionality

The next task is to interact with the user to implement the menu functionality. Dialogs are used to get immediate input from the user. Dialogs are application modal, i.e. you can not use any other windows in the current application until the dialogue is closed.


def OnMenuFileOpenMenu(self, event):
        dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", wx.OPEN)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                filename = dlg.GetPath()
                # Your code
        finally:
            dlg.Destroy()
        event.Skip()




def OnMenuFileOpenMenu(self, event):
        dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", wx.OPEN)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                filename = dlg.GetPath()
                # Your code
                self.textEditor.LoadFile(filename)
                self.FileName=filename
                self.SetTitle(('Notebook - %s') % filename)        
        finally:
            dlg.Destroy()



def OnMenuFileSaveasMenu(self, event):
        dlg = wx.FileDialog(self, "Save file as", ".", "", "*.*", wx.SAVE)
        try:
            if dlg.ShowModal() == wx.ID_OK:
                filename = dlg.GetPath()
                # Your code
                self.textEditor.SaveFile(filename)
                self.FileName=filename
                self.SetTitle(('Notebook - %s') % filename)     
        finally:
            dlg.Destroy()



def OnMenuFileCloseMenu(self, event):
        self.FileName = None
        self.textEditor.Clear()
        self.SetTitle('Notebook')



def OnMenuFileExitMenu(self, event):
        self.Close()



def __init__(self, parent):
        self._init_ctrls(parent)
        self.FileName=None



def OnMenuFileSaveMenu(self, event):
        if self.FileName == None:
            return self.OnFileSaveasMenu(event)
        else:
            self.textEditor.SaveFile(self.FileName)