How to define a default dir for save button

Hi, I am embeding a matplotlib FigureCanvasWxAgg in a

    > wxPython program.

    > When i press the save botton the directory is that of
    > the executable of the program. I want to change this to
    > a certain directory. How can i do that?

There may be an intelligent way to set the default dir globally for file
dialogs in wx, but I don't know what it is. So I'll suggest a workaround.

You can create a custom toolbar and override save

from matplotlib.backends.backend_wx import NavigationToolbar2Wx
class MyToolbar(class NavigationToolbar2Wx):
    def save(self, evt):
        # Fetch the required filename and file type.
        filetypes = self.canvas._get_imagesave_wildcards()
        # insert your default dir here
        dlg =wx.FileDialog(self._parent, "Save to file", "", "", filetypes,
                           wx.SAVE|wx.OVERWRITE_PROMPT|wx.CHANGE_DIR)
        if dlg.ShowModal() == wx.ID_OK:

            dirname = dlg.GetDirectory()
            filename = dlg.GetFilename()
            DEBUG_MSG('Save file dir:%s name:%s' % (dirname, filename), 3, self)
            self.canvas.print_figure(os.path.join(dirname, filename))

and then use this toolbar rather than the default one. See
examples/embedding_in_wx4.py for an example of how to use a custom
toolbar in a wx app.

JDH

John Hunter wrote:

There may be an intelligent way to set the default dir globally for file
dialogs in wx, but I don't know what it is. So I'll suggest a workaround.

I don't think there isn't. However, it probably starts out in the current working directory, which you can change with:

import os
os.chdir("ThePathYouWant")

John's solution is better if this is more than a quickie app, however.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...