# 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.

- Dialogs are placed directly into the source code. The are not placed by the Designer. They are placed with the editor. In the wxFrame1.py source code, go the the event handler for the open event. This method is called OnFileitems0Menu. We are going to place the 'File Open' dialogue into this method. Place the keyboard cursor directly before the word pass. We are going to insert our new code here.
- On the palette, select the 'Dialogs' pane. Select the 'wxFileDialog' button. When you press on the wxFileDialog button Boa Constructor will paste skeleton code directly into your Event Handler method. The skeleton code is shown below.

        def OnFileitems0Menu(self, event):
            dlg = wxFileDialog(self, "Choose a file", ".", "", "*.*", wxOPEN)
            try:
                if dlg.ShowModal() == wxID_OK:
                    filename = dlg.GetPath()
                    # Your code
            finally:
                dlg.Destroy()
            pass

- This skeleton code creates a dialog. It interact with the user. When the dialog is finished it is destroyed.
- The words '# Your code' mark the position where we are to insert our own code. This code is triggered only when the user chooses or enters a filename. We will insert our code here. The wxTextCtrl has a method which we will use to load a file into the edit window. The method is 'LoadFile'.
- You can delete the trailing 'pass' here if you want. We no longer need it. It is needed when the code is generated because Python reports and error if there is a method with no body.
- As part of the functionality of our application we must save the file name so that the 'Save' menu option can save to this file. The listing below shows our new code.

        def OnFileitems0Menu(self, event):
            dlg = wxFileDialog(self, "Choose a file", ".", "", "*.*", wxOPEN)
            try:
                if dlg.ShowModal() == wxID_OK:
                    filename = dlg.GetPath()
                    self.txtEditor.LoadFile(filename)
                    self.FileName=filename
            finally:
                dlg.Destroy()

- We must repeat the exercise to provide the 'Save As' functionality. The save as menu item was item2. Insert a file dialog into the body of OnFileitems2Menu.
- This is a File Save dialog. Change the prompt, parameter 2 to the wxFileDialog to "Save File As". Change the style, parameter 6 to wxSAVE. The method to save the file is called SaveFile.
- Again, we save the filename value for use by the 'Save' menu option. The listing below shows the code.

        def OnFileitems2Menu(self, event):
            dlg = wxFileDialog(self, "Save File As", ".", "", "*.*", wxSAVE)
            try:
                if dlg.ShowModal() == wxID_OK:
                    filename = dlg.GetPath()
                    self.txtEditor.SaveFile(filename)
                    self.FileName=filename
            finally:
                dlg.Destroy()

- Next we will implement the 'Close' menu option. In this method, we simply clear the txtEditor screen field, and the FileName member variable.

        def OnFileitems3Menu(self, event):

            self.FileName = None
            self.txtEditor.Clear()

- Next we will implement the 'Exit' menu option. In this method we need to terminate the application. All wxPython applications are terminated by closing the toplevel window. In our case we only have the wxFrame1 window. To terminate the application we invoke the Close() method for wxFrame1.

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

- Next we will implement the 'Save' menu item. This menu item will save the file using the current name, which is stored in the variable self.FileName.
- Where there is no current file, the self.FileName variable is set to None. In this case the 'Save' menu option must act as the 'Save As' menu option.
- The variable FileName must be created when wxFrame is constructed. We must add it to the constructor. You can add your application code to the end of the default constructor (__init__).

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

- Now, we are safe to implement the Save functionality. We check if there is a current filename. If there is we can save the contents to that filename. Otherwise, we must call the 'Save As' method.

        def OnFileitems1Menu(self, event):
            if self.FileName == None:
                return self.OnFileitems2Menu(event)
            else:
                self.txtEditor.SaveFile(self.FileName)

We have now implemented the functionality of the editor. We can edit files, save them. Your editor should look like this: