.. include:: headings.inc .. _FileDialog: ========================================================================================================================================== |phoenix_title| **FileDialog** ========================================================================================================================================== This class represents the file chooser dialog. The path and filename are distinct elements of a full file pathname. If path is ``''`` the current directory will be used. If filename is ``''`` no default filename will be supplied. The wildcard determines what files are displayed in the file selector, and file extension supplies a type extension for the required filename. The typical usage for the open file dialog is: :: def OnOpen(self, event): if self.contentNotSaved: if wx.MessageBox("Current content has not been saved! Proceed?", "Please confirm", wx.ICON_QUESTION | wx.YES_NO, self) == wx.NO: return # else: proceed asking to the user the new file to open openFileDialog = wx.FileDialog(self, "Open XYZ file", "", "", "XYZ files (*.xyz)|*.xyz", wx.FD_OPEN|.FD_FILE_MUST_EXIST) if openFileDialog.ShowModal() == wx.ID_CANCEL: return # the user changed idea... # proceed loading the file chosen by the user # this can be done with e.g. wxPython input streams: input_stream = wx.FileInputStream(openFileDialog.GetPath()) if not input_stream.IsOk(): wx.LogError("Cannot open file '%s'."%openFileDialog.GetPath()) return The typical usage for the save file dialog is instead somewhat simpler: :: def OnSaveAs(self, event): saveFileDialog = wx.FileDialog(self, "Save XYZ file", "", "", "XYZ files (*.xyz)|*.xyz", wx.FD_SAVE|.FD_OVERWRITE_PROMPT) if saveFileDialog.ShowModal() == wx.ID_CANCEL: return # the user changed idea... # save the current contents in the file # this can be done with e.g. wxPython output streams: output_stream = wx.FileOutputStream(saveFileDialog.GetPath()) if not output_stream.IsOk(): wx.LogError("Cannot save current contents in file '%s'."%saveFileDialog.GetPath()) return .. _FileDialog-styles: |styles| Window Styles ================================ This class supports the following styles: - ``FD_DEFAULT_STYLE``: Equivalent to ``FD_OPEN`` . - ``FD_OPEN``: This is an open dialog; usually this means that the default button's label of the dialog is "Open". Cannot be combined with ``FD_SAVE`` . - ``FD_SAVE``: This is a save dialog; usually this means that the default button's label of the dialog is "Save". Cannot be combined with ``FD_OPEN`` . - ``FD_OVERWRITE_PROMPT``: For save dialog only: prompt for a confirmation if a file will be overwritten. - ``FD_FILE_MUST_EXIST``: For open dialog only: the user may only select files that actually exist. Notice that under OS X the file dialog with ``FD_OPEN`` style always behaves as if this style was specified, because it is impossible to choose a file that doesn't exist from a standard OS X file dialog. - ``FD_MULTIPLE``: For open dialog only: allows selecting multiple files. - ``FD_CHANGE_DIR``: Change the current working directory (when the dialog is dismissed) to the directory where the file(s) chosen by the user are. - ``FD_PREVIEW``: Show the preview of the selected files (currently only supported by wxGTK). .. note:: All implementations of the :ref:`FileDialog` provide a wildcard filter. Typing a filename containing wildcards (, ?) in the filename text item, and clicking on Ok, will result in only those files matching the pattern being displayed. The wildcard may be a specification for multiple types of file with a description for each, such as: :: wildcard = "BMP and GIF files (*.bmp*.gif)|*.bmp*.gif|PNG files (*.png)|*.png" It must be noted that wildcard support in the native Motif file dialog is quite limited: only one file type is supported, and it is displayed without the descriptive test; "``BMP`` files (.bmp)|.bmp" is displayed as ".bmp", and both "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif" and "Image files|.bmp;.gif" are errors. .. seealso:: :ref:`FileDialog Overview `, :func:`FileSelector` | |class_hierarchy| Inheritance Diagram ===================================== Inheritance diagram for class **FileDialog** .. raw:: html

Inheritance diagram of FileDialog

| |method_summary| Methods Summary ================================ ================================================================================ ================================================================================ :meth:`~FileDialog.__init__` Constructor. :meth:`~FileDialog.GetDirectory` Returns the default directory. :meth:`~FileDialog.GetExtraControl` If functions :meth:`SetExtraControlCreator` and :meth:`ShowModal` were called, returns the extra window. :meth:`~FileDialog.GetFilename` Returns the default filename. :meth:`~FileDialog.GetFilterIndex` Returns the index into the list of filters supplied, optionally, in the wildcard parameter. :meth:`~FileDialog.GetMessage` Returns the message that will be displayed on the dialog. :meth:`~FileDialog.GetPath` Returns the full path (directory and filename) of the selected file. :meth:`~FileDialog.GetWildcard` Returns the file dialog wildcard. :meth:`~FileDialog.SetDirectory` Sets the default directory. :meth:`~FileDialog.SetFilename` Sets the default filename. :meth:`~FileDialog.SetFilterIndex` Sets the default filter index, starting from zero. :meth:`~FileDialog.SetMessage` Sets the message that will be displayed on the dialog. :meth:`~FileDialog.SetPath` Sets the path (the combined directory and filename that will be returned when the dialog is dismissed). :meth:`~FileDialog.SetWildcard` Sets the wildcard, which can contain multiple file types, for example: "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif". :meth:`~FileDialog.ShowModal` Shows the dialog, returning ``ID_OK`` if the user pressed ``OK``, and ``ID_CANCEL`` otherwise. ================================================================================ ================================================================================ | |property_summary| Properties Summary ===================================== ================================================================================ ================================================================================ :attr:`~FileDialog.Directory` See :meth:`~FileDialog.GetDirectory` and :meth:`~FileDialog.SetDirectory` :attr:`~FileDialog.ExtraControl` See :meth:`~FileDialog.GetExtraControl` :attr:`~FileDialog.Filename` See :meth:`~FileDialog.GetFilename` and :meth:`~FileDialog.SetFilename` :attr:`~FileDialog.Filenames` See :meth:`~FileDialog.GetFilenames` :attr:`~FileDialog.FilterIndex` See :meth:`~FileDialog.GetFilterIndex` and :meth:`~FileDialog.SetFilterIndex` :attr:`~FileDialog.Message` See :meth:`~FileDialog.GetMessage` and :meth:`~FileDialog.SetMessage` :attr:`~FileDialog.Path` See :meth:`~FileDialog.GetPath` and :meth:`~FileDialog.SetPath` :attr:`~FileDialog.Paths` See :meth:`~FileDialog.GetPaths` :attr:`~FileDialog.Wildcard` See :meth:`~FileDialog.GetWildcard` and :meth:`~FileDialog.SetWildcard` ================================================================================ ================================================================================ | |api| Class API =============== .. class:: FileDialog(Dialog) This class represents the file chooser dialog. **Possible constructors**:: FileDialog(parent, message=FileSelectorPromptStr, defaultDir='', defaultFile='', wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE, pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr) .. method:: __init__(self, parent, message=FileSelectorPromptStr, defaultDir='', defaultFile='', wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE, pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr) Constructor. Use :meth:`ShowModal` to show the dialog. :param `parent`: Parent window. :type `parent`: Window :param `message`: Message to show on the dialog. :type `message`: string :param `defaultDir`: The default directory, or the empty string. :type `defaultDir`: string :param `defaultFile`: The default filename, or the empty string. :type `defaultFile`: string :param `wildcard`: A wildcard, such as "." or "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif". Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above. :type `wildcard`: string :param `style`: A dialog style. See ``FD_*`` styles for more info. :type `style`: long :param `pos`: Dialog position. Not implemented. :type `pos`: Point :param `size`: Dialog size. Not implemented. :type `size`: Size :param `name`: Dialog name. Not implemented. :type `name`: string .. method:: GetDirectory(self) Returns the default directory. :rtype: `string` .. method:: GetExtraControl(self) If functions :meth:`SetExtraControlCreator` and :meth:`ShowModal` were called, returns the extra window. Otherwise returns ``None``. :rtype: :ref:`Window` .. versionadded:: 2.9.0 .. method:: GetFilename(self) Returns the default filename. :rtype: `string` .. method:: GetFilterIndex(self) Returns the index into the list of filters supplied, optionally, in the wildcard parameter. Before the dialog is shown, this is the index which will be used when the dialog is first displayed. After the dialog is shown, this is the index selected by the user. :rtype: `int` .. method:: GetMessage(self) Returns the message that will be displayed on the dialog. :rtype: `string` .. method:: GetPath(self) Returns the full path (directory and filename) of the selected file. :rtype: `string` .. method:: GetWildcard(self) Returns the file dialog wildcard. :rtype: `string` .. method:: SetDirectory(self, directory) Sets the default directory. :param `directory`: :type `directory`: string .. method:: SetFilename(self, setfilename) Sets the default filename. In wxGTK this will have little effect unless a default directory has previously been set. :param `setfilename`: :type `setfilename`: string .. method:: SetFilterIndex(self, filterIndex) Sets the default filter index, starting from zero. :param `filterIndex`: :type `filterIndex`: int .. method:: SetMessage(self, message) Sets the message that will be displayed on the dialog. :param `message`: :type `message`: string .. method:: SetPath(self, path) Sets the path (the combined directory and filename that will be returned when the dialog is dismissed). :param `path`: :type `path`: string .. method:: SetWildcard(self, wildCard) Sets the wildcard, which can contain multiple file types, for example: "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif". Note that the native Motif dialog has some limitations with respect to wildcards; see the Remarks section above. :param `wildCard`: :type `wildCard`: string .. method:: ShowModal(self) Shows the dialog, returning ``ID_OK`` if the user pressed ``OK``, and ``ID_CANCEL`` otherwise. :rtype: `int` .. attribute:: Directory See :meth:`~FileDialog.GetDirectory` and :meth:`~FileDialog.SetDirectory` .. attribute:: ExtraControl See :meth:`~FileDialog.GetExtraControl` .. attribute:: Filename See :meth:`~FileDialog.GetFilename` and :meth:`~FileDialog.SetFilename` .. attribute:: Filenames See :meth:`~FileDialog.GetFilenames` .. attribute:: FilterIndex See :meth:`~FileDialog.GetFilterIndex` and :meth:`~FileDialog.SetFilterIndex` .. attribute:: Message See :meth:`~FileDialog.GetMessage` and :meth:`~FileDialog.SetMessage` .. attribute:: Path See :meth:`~FileDialog.GetPath` and :meth:`~FileDialog.SetPath` .. attribute:: Paths See :meth:`~FileDialog.GetPaths` .. attribute:: Wildcard See :meth:`~FileDialog.GetWildcard` and :meth:`~FileDialog.SetWildcard`