OnSave() and OnOpen()

This wiki page <https://wxpython.org/Phoenix/docs/html/wx.FileDialog.html>
has an example of an OnSaveAs() method:

def OnSaveAs(self, event):
     with wx.FileDialog(self, "Save XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                        style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:

         if fileDialog.ShowModal() == wx.ID_CANCEL:
             return # the user changed their mind

         # save the current contents in the file
         pathname = fileDialog.GetPath()
         try:
             with open(pathname, 'w') as file:
                 self.doSaveData(file)
         except IOError:
             wx.LogError("Cannot save current data in file '%s'." % pathname)

   When that method is invoked there is a text entry widget on the top of the
dialog box with the label, Name. I don't see where that widget and label are
specified. What am I missing here?

   On a related method, OnOpen(), the wx.FileDialog() is called,

with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                        style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

   If I want to save the file handle of the open file to a variable, e.g.,
fh, where to I make that the left side of the expression? Prepending 'with'
using 'fh = ' throws an error.

Rich

Rich Shepard wrote:

This wiki page
<https://wxpython.org/Phoenix/docs/html/wx.FileDialog.html&gt;
has an example of an OnSaveAs() method:

def OnSaveAs(self, event):
with wx.FileDialog(self, "Save XYZ file", wildcard="XYZ files
(*.xyz)|*.xyz",
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as
fileDialog:
...

When that method is invoked there is a text entry widget on the top
of the
dialog box with the label, Name. I don't see where that widget and
label are
specified. What am I missing here?

What operating system are you running? Usually, wx.FileDialog calls a
system-supplied common dialog, so it should be totally familiar to
users. Can you show a screenshot of what you see?

On a related method, OnOpen(), the wx.FileDialog() is called,

with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files
(*.xyz)|*.xyz",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as
fileDialog:

If I want to save the file handle of the open file to a variable, e.g.,
fh, where to I make that the left side of the expression? Prepending
'with'
using 'fh = ' throws an error.

The wx.FileDialog does not actually open the file. It merely supplies
the path name, in fileDialog.GetPath(). It's up to you to open the file
and save the handle, if you need it for longer than just this
invocation. You can see this on the same page you mentioned above.
Instead of
with open(pathname,'r') as file:
you could use
self.openFile = open(pathname,'r')

However, you should think about your design. It is quite common for an
OnOpen handler to ask for the file name, read the contents into state,
and close the file, so knowledge of the file itself does not leak
outside that one function. You wouldn't ordinarily save the open file
handle (although, of course, there are circumstances where that does
make sense). Remember, OnOpen is commonly called in response to a File
-> Open menu item. If you've saved the open file handle, who is going
to use it?

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

What operating system are you running? Usually, wx.FileDialog calls a
system-supplied common dialog, so it should be totally familiar to users.
Can you show a screenshot of what you see?

Hi Tim,

   I run Slackware-14.2. Screen shot of OnSaveAs() attached.

The wx.FileDialog does not actually open the file. It merely supplies the
path name, in fileDialog.GetPath(). It's up to you to open the file and
save the handle, if you need it for longer than just this invocation. You
can see this on the same page you mentioned above. Instead of with
open(pathname,'r') as file: you could use self.openFile =
open(pathname,'r')

   That makes sense.

   I want the file handle so the user can use ctrl-s to save the file while
working on it.

   However, I realize that I've designed the menu for a non-SQLAlchemy
application and I need to read more of the SA docs to determine what menu
options are needed or desired.

Thanks,

Rich

···

On Tue, 1 May 2018, Tim Roberts wrote:

Rich Shepard wrote:

What operating system are you running? Usually, wx.FileDialog calls a
system-supplied common dialog, so it should be totally familiar to
users.
Can you show a screenshot of what you see?

I run Slackware-14.2. Screen shot of OnSaveAs() attached.

I don't know what desktop or graphics environment Slackware uses, but
that must be their standard "Save File" dialog. That field is where you
can type the name, for those of us who prefer keyboard ops to mouse
ops. The name of the field is probably not customizable (although it's
probably localized for you).

I want the file handle so the user can use ctrl-s to save the file
while
working on it.

Then you certainly don't want to keep the file open the whole time. You
should just save the file name. You can open it for "read" at open
time, and open it for "write" as save time. The handle should be
considered transitory.

···

On Tue, 1 May 2018, Tim Roberts wrote:

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.