wxPython New, Save, and SaveAs Methods
Question: I'm writing a UI for a python app with wxPython. I've handled a few of the OnX
functions but I need help with OnNew and OnSave/SaveAs

Here is my Save and SaveAs code:

    
    
    def OnSave(self, event):
        self.dirname = ""
        saveFileDialog = wx.FileDialog(self, "Save Operation File", self.dirname, "",
            "Operation Files (*.fwr)|*.fwr|All Files (*.*)|*.*", wx.SAVE|wx.OVERWRITE_PROMPT)
        if saveFileDialog.ShowModal() == wx.ID_OK:
            contents = self.control.GetValue()
            self.filename = saveFileDialog.GetFilename()
            self.dirname = saveFileDialog.GetDirectory()
            filehandle = open(os.path.join(self.dirname, self.filename), 'w')
            filehandle.write(contents)
            filehandle.close()
        else:
            sys.exit(1)
        saveFileDialog.Destroy()
    
    def OnSaveAs(self, event):
        self.dirname = "";
    
        saveAsFileDialog = wx.FileDialog(self, "Save Operation File As", self.dirname, "",
            "Operation Files (*.fwr)|*.fwr|All Files (*.*)|*.*", 
            wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
    
        if saveAsFileDialog.ShowModal() == wx.ID_OK:
            contents = self.control.GetValue()
            self.filename = saveFileDialog.GetFilename()
            self.dirname = saveFileDialog.GetDirectory()
            filehandle = open(os.path.join(self.dirname, self.filename), 'w')
            filehandle.write(contents)
            filehandle.close()
        else:
            sys.exit(1)
        saveFileDialog.Destroy()
    
        # save current contents in the file
        # use wxPython output streams
        #output_stream = wx.FileOutputStream(saveFileDialog.GetPath())
    
        #if not output_stream.IsOk():    
        #    wx.LogError("Cannot save contents of Operations File '%s'" % saveFileDialog.GetPath())
        #    return  
    

the commented part at the bottom is another way I found to do it, is using the
input and output streams more correct than the way it currently is? Also heres
my other question, I got OnNew Working, here is the code:

    
    
    def OnNew(self,  event):
        homedir = os.environ['HOME']
        if not os.path.exists(homedir):
            if getpass.getuser():
                homedir = "C:/Users/" + getpass.getuser() + "/"
            else:
                homedir = "C:/"
        newFileDialog = wx.FileDialog(self, "New Operation File", homedir, "",
            "Operation Files (*.fwr)|*.fwr|All Files|(*.*)|*.*", wx.FD_CREATE|wx.OVERWRITE_PROMPT)
    

Everything is great but the OnOpen Method opens an open file dialog, I want a
create file dialog (would this be the same as save? Can someone give me an
example OnOpen method, and give me some insight into my OnSave and OnSaveAs
methods? As you can see there are three aproaches, one in OnSaveAs, one in
OnSave, and one commented out at the bottom of OnSaveAs(). As well as many
more I didn't write down here. My main question though is how to get the
filedialog for new to be a save dialog where you create a file, rather than an
open dialog.

Thanks a ton.

SUMMARY:

1) How do you bring up a FileDialog that allows creation of blank files. I
assum it will be similar to save, but hwatever ID flags I pass it always gives
me an Open button

2) And as for save methods, is it better to do what I showed in the code, or
use streams like the commented out section in SaveAs?.


Answer: To get the Save dialog, you need to pass the `wx.SAVE` style flag to your
`FileDialog` object: `style=wx.SAVE`. You can read more about the save flag
[here](http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-
part-1-of-2/) or [here](http://stackoverflow.com/questions/14524525/implement-
save-as-in-save-dialog-wxpython).

Here's some example code that worked for me on Xubuntu 14.04 with wxPython
2.8.12.1 and Python 2.7:

    
    
    import os
    import wx
    
    wildcard = "Python source (*.py)|*.py|" \
                "All files (*.*)|*.*"
    
    ########################################################################
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY,
                              "File and Folder Dialogs Tutorial")
            panel = wx.Panel(self, wx.ID_ANY)
            self.currentDirectory = os.getcwd()
    
            saveFileDlgBtn = wx.Button(panel, label="Show SAVE FileDialog")
            saveFileDlgBtn.Bind(wx.EVT_BUTTON, self.onSaveFile)
    
            # put the buttons in a sizer
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(saveFileDlgBtn, 0, wx.ALL|wx.CENTER, 5)
            panel.SetSizer(sizer)
    
        #----------------------------------------------------------------------
        def onSaveFile(self, event):
            """
            Create and show the Save FileDialog
            """
            dlg = wx.FileDialog(
                self, message="Save file as ...", 
                defaultDir=self.currentDirectory, 
                defaultFile="", wildcard=wildcard, style=wx.SAVE
                )
            if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                print "You chose the following filename: %s" % path
            dlg.Destroy()
    
    #----------------------------------------------------------------------
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

I don't see anything wrong with your saving approach. In most cases, it's
better to use Python's low level operators instead of using wxPython's. I
would use Python's `with` operator though as that follows the newer idiom
better:

    
    
    with open(os.path.join(self.dirname, self.filename), 'w') as filehandle:
        filehandle.write(contents)
    

