.. wxPython Phoenix documentation

   This file was generated by Phoenix's sphinx generator and associated
   tools, do not edit by hand.

   Copyright: (c) 2011-2020 by Total Control Software
   License:   wxWindows License

.. include:: headings.inc



.. _wx.FileDialog:

==========================================================================================================================================
|phoenix_title|  **wx.FileDialog**
==========================================================================================================================================

This class represents the file chooser dialog.          

The path and filename are distinct elements of a full file pathname. If path is :meth:`~wx.FileDialog.EmptyString`, the current directory will be used. If filename is :meth:`~wx.FileDialog.EmptyString`, 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

            # otherwise ask the user what new file to open
            with wx.FileDialog(self, "Open XYZ file", wildcard="XYZ files (*.xyz)|*.xyz",
                               style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

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

                # Proceed loading the file chosen by the user
                pathname = fileDialog.GetPath()
                try:
                    with open(pathname, 'r') as file:
                        self.doLoadDataOrWhatever(file)
                except IOError:
                    wx.LogError("Cannot open file '%s'." % newfile)



The typical usage for the save file dialog is instead somewhat simpler: ::

        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)





|phoenix_title| Wildcard Filters
================================

All implementations of the :ref:`wx.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"

On Mac macOS in the open file dialog the filter choice box is not shown by default. Instead all given wildcards are applied at the same time: So in the above example all bmp, gif and png files are displayed. To enforce the display of the filter choice set the corresponding :ref:`wx.SystemOptions`  before calling the file open dialog: ::

    wx.SystemOptions.SetOption(wx.OSX_FILEDIALOG_ALWAYS_SHOW_TYPES, 1)

But in contrast to Windows and Unix, where the file type choice filters only the selected files, on Mac macOS even in this case the dialog shows all files matching all file types. The files which does not match the currently selected file type are greyed out and are not selectable.  



|phoenix_title| Dialog Customization
====================================



.. _FileDialog-styles:

|styles| Window Styles
================================

Uniquely among the other standard dialogs, :ref:`wx.FileDialog`  can be customized by adding extra controls to it. Moreover, there are two ways to do it: the first one is to define a callback function and use :meth:`~wx.FileDialog.SetExtraControlCreator`  to tell the dialog to call it, while the second one requires defining a class inheriting from :ref:`wx.FileDialogCustomizeHook`  and implementing its virtual functions, notably :meth:`wx.FileDialogCustomizeHook.AddCustomControls`   where the extra controls have to be created, and finally calling :meth:`~wx.FileDialog.SetCustomizeHook`  with this custom hook object. The first approach is somewhat simpler and more flexible, as it allows to create any kind of custom controls, but is not supported by the "new style" (where "new" means used since Windows Vista, i.e. circa 2007) file dialogs under MSW. Because of this, calling :meth:`~wx.FileDialog.SetExtraControlCreator`  in wxMSW forces the use of old style (Windows XP) dialogs, that may look out of place. The second approach is implemented by the MSW dialogs natively and doesn't suffer from this limitation, so its use is recommended, especially if the few simple control types supported by it (see :ref:`wx.FileDialogCustomize`  for more information about the supported controls) are sufficient for your needs. Both of the approaches to the dialog customization are demonstrated in the :ref:`Dialogs Sample <dialogs sample>`, please check it for more details. ^^ This class supports the following styles:   

- ``wx.FD_DEFAULT_STYLE``: Equivalent to  ``FD_OPEN`` .   
- ``wx.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`` .   
- ``wx.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`` .   
- ``wx.FD_OVERWRITE_PROMPT``: For save dialog only: prompt for a confirmation if a file will be overwritten. This style is always enabled on wxOSX and cannot be disabled.  
- ``wx.FD_NO_FOLLOW``: Directs the dialog to return the path and file name of the selected shortcut file, not its target as it does by default. Currently this flag is only implemented in wxMSW and wxOSX (where it prevents aliases from being resolved). The non-dereferenced link path is always returned, even without this flag, under Unix and so using it there doesn't do anything. This flag was added in wxWidgets 3.1.0.  
- ``wx.FD_FILE_MUST_EXIST``: For open dialog only: the user may only select files that actually exist. Notice that under macOS 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 macOS file dialog.   
- ``wx.FD_MULTIPLE``: For open dialog only: allows selecting multiple files.  
- ``wx.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.  
- ``wx.FD_PREVIEW``: Show the preview of the selected files (currently only supported by wxGTK).  
- ``wx.FD_SHOW_HIDDEN``: Show hidden files. This flag was added in wxWidgets 3.1.3 ^^ 






.. seealso:: :ref:`FileDialog Overview <filedialog overview>`, :ref:`wx.FileSelector`    







|

|class_hierarchy| Class Hierarchy
=================================

.. raw:: html

   <div id="toggleBlock" onclick="return toggleVisibility(this)" class="closed" style="cursor:pointer;">
   <img id="toggleBlock-trigger" src="_static/images/closed.png"/>
   Inheritance diagram for class <strong>FileDialog</strong>:
   </div>
   <div id="toggleBlock-summary" style="display:block;"></div>
   <div id="toggleBlock-content" style="display:none;">
   <p class="graphviz">
   <center><img src="_static/images/inheritance/wx.FileDialog_inheritance.png" alt="Inheritance diagram of FileDialog" usemap="#dummy" class="inheritance"/></center>
   <script type="text/javascript">toggleVisibilityOnLoad(document.getElementById('toggleBlock'))</script>
   <map id="dummy" name="dummy"> <area shape="rect" id="node1" href="wx.FileDialog.html" title="wx.FileDialog" alt="" coords="54,469,163,499"/> <area shape="rect" id="node2" href="wx.Dialog.html" title="wx.Dialog" alt="" coords="65,392,152,421"/> <area shape="rect" id="node3" href="wx.TopLevelWindow.html" title="wx.TopLevelWindow" alt="" coords="31,315,186,344"/> <area shape="rect" id="node4" href="wx.NonOwnedWindow.html" title="wx.NonOwnedWindow" alt="" coords="25,237,193,267"/> <area shape="rect" id="node5" href="wx.Window.html" title="wx.Window" alt="" coords="60,160,157,189"/> <area shape="rect" id="node6" href="wx.EvtHandler.html" title="wx.EvtHandler" alt="" coords="50,83,167,112"/> <area shape="rect" id="node7" href="wx.Object.html" title="wx.Object" alt="" coords="5,5,92,35"/> <area shape="rect" id="node8" href="wx.Trackable.html" title="wx.Trackable" alt="" coords="117,5,223,35"/> </map> 
   </p>
   </div>

|


|method_summary| Methods Summary
================================

================================================================================ ================================================================================
:meth:`~wx.FileDialog.__init__`                                                  Constructor.
:meth:`~wx.FileDialog.AddShortcut`                                               Add a directory to the list of shortcuts shown in the dialog.
:meth:`~wx.FileDialog.GetClassDefaultAttributes`                                 
:meth:`~wx.FileDialog.GetCurrentlySelectedFilename`                              Returns the path of the file currently selected in dialog.
:meth:`~wx.FileDialog.GetCurrentlySelectedFilterIndex`                           Returns the file type filter index currently selected in dialog.
:meth:`~wx.FileDialog.GetDirectory`                                              Returns the default directory.
:meth:`~wx.FileDialog.GetExtraControl`                                           If functions :meth:`~FileDialog.SetExtraControlCreator`   and :meth:`~FileDialog.ShowModal`   were called, returns the extra window.
:meth:`~wx.FileDialog.GetFilename`                                               Returns the default filename.
:meth:`~wx.FileDialog.GetFilenames`                                              Returns a list of filenames chosen in the dialog.  This function
:meth:`~wx.FileDialog.GetFilterIndex`                                            Returns the index into the list of filters supplied, optionally, in the wildcard parameter.
:meth:`~wx.FileDialog.GetMessage`                                                Returns the message that will be displayed on the dialog.
:meth:`~wx.FileDialog.GetPath`                                                   Returns the full path (directory and filename) of the selected file.
:meth:`~wx.FileDialog.GetPaths`                                                  Returns a list of the full paths of the files chosen. This function
:meth:`~wx.FileDialog.GetWildcard`                                               Returns the file dialog wildcard.
:meth:`~wx.FileDialog.SetCustomizeHook`                                          Set the hook to be used for customizing the dialog contents.
:meth:`~wx.FileDialog.SetDirectory`                                              Sets the default directory.
:meth:`~wx.FileDialog.SetFilename`                                               Sets the default filename.
:meth:`~wx.FileDialog.SetFilterIndex`                                            Sets the default filter index, starting from zero.
:meth:`~wx.FileDialog.SetMessage`                                                Sets the message that will be displayed on the dialog.
:meth:`~wx.FileDialog.SetPath`                                                   Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).
:meth:`~wx.FileDialog.SetWildcard`                                               Sets the wildcard, which can contain multiple file types, for example: "``BMP`` files (.bmp)|.bmp|GIF files (.gif)|.gif".
:meth:`~wx.FileDialog.ShowModal`                                                 Shows the dialog, returning  ``ID_OK``   if the user pressed ``wx.OK``, and   ``ID_CANCEL``   otherwise.
================================================================================ ================================================================================


|


|property_summary| Properties Summary
=====================================

================================================================================ ================================================================================
:attr:`~wx.FileDialog.CurrentlySelectedFilename`                                 See :meth:`~wx.FileDialog.GetCurrentlySelectedFilename`
:attr:`~wx.FileDialog.CurrentlySelectedFilterIndex`                              See :meth:`~wx.FileDialog.GetCurrentlySelectedFilterIndex`
:attr:`~wx.FileDialog.Directory`                                                 See :meth:`~wx.FileDialog.GetDirectory` and :meth:`~wx.FileDialog.SetDirectory`
:attr:`~wx.FileDialog.ExtraControl`                                              See :meth:`~wx.FileDialog.GetExtraControl`
:attr:`~wx.FileDialog.Filename`                                                  See :meth:`~wx.FileDialog.GetFilename` and :meth:`~wx.FileDialog.SetFilename`
:attr:`~wx.FileDialog.Filenames`                                                 See :meth:`~wx.FileDialog.GetFilenames`
:attr:`~wx.FileDialog.FilterIndex`                                               See :meth:`~wx.FileDialog.GetFilterIndex` and :meth:`~wx.FileDialog.SetFilterIndex`
:attr:`~wx.FileDialog.Message`                                                   See :meth:`~wx.FileDialog.GetMessage` and :meth:`~wx.FileDialog.SetMessage`
:attr:`~wx.FileDialog.Path`                                                      See :meth:`~wx.FileDialog.GetPath` and :meth:`~wx.FileDialog.SetPath`
:attr:`~wx.FileDialog.Paths`                                                     See :meth:`~wx.FileDialog.GetPaths`
:attr:`~wx.FileDialog.Wildcard`                                                  See :meth:`~wx.FileDialog.GetWildcard` and :meth:`~wx.FileDialog.SetWildcard`
================================================================================ ================================================================================


|


|api| Class API
===============


.. class:: wx.FileDialog(Dialog)

   **Possible constructors**::

       FileDialog(parent, message=FileSelectorPromptStr,
                  defaultDir=EmptyString, defaultFile=EmptyString,
                  wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE,
                  pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr)
       
   
   This class represents the file chooser dialog.



   .. method:: __init__(self, parent, message=FileSelectorPromptStr, defaultDir=EmptyString, defaultFile=EmptyString, 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`: wx.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 "x.x" 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`: wx.Point
      :param `size`: Dialog size. Not implemented.   
      :type `size`: wx.Size
      :param `name`: Dialog name. Not implemented.   
      :type `name`: string




















                  





   .. method:: AddShortcut(self, directory, flags=0)

      Add a directory to the list of shortcuts shown in the dialog.                  

      File dialogs on many platforms display a fixed list of directories which can be easily selected by the user. This function allows to add an application-defined directory to this list, which can be convenient for the programs that use specific directories for their files instead of the default user document directory (see :ref:`wx.StandardPaths`). 

      Currently this function is only implemented in wxMSW and wxGTK and does nothing under the other platforms. Moreover, in wxMSW this function is incompatible with the use of :meth:`SetExtraControlCreator` , if you need to use this function and customize the dialog contents, please use the newer :meth:`SetCustomizeHook`   instead. 

      The :ref:`dialogs sample <dialogs sample>`  shows the use of this function by adding two custom shortcuts corresponding to the subdirectories of  ``WXWIN``   environment variable if it is defined. 




      :param `directory`: The full path to the directory, which should exist.   
      :type `directory`: string
      :param `flags`: Can be set to  ``FD_SHORTCUT_BOTTOM``   (which is also the default behaviour) to add the shortcut after the existing ones, or   ``FD_SHORTCUT_TOP``   to add it before them. Support for the latter flag is only available in wxMSW, in wxGTK the shortcuts are always added to the bottom of the list.    
      :type `flags`: int








      :rtype: `bool`



                  



      :returns: 

         ``True`` on success or ``False`` if shortcut couldn't be added, e.g. because this functionality is not available on the current platform.  







      .. versionadded:: 4.2/wxWidgets-3.2.1  
     







      .. note:: 

         In wxMSW, the shortcuts appear in a separate section called "Application Links" by default. To change the title of this section, the application can specify a value of the  ``FileDescription``   field of the version information structure in its resource file  –  








   .. staticmethod:: GetClassDefaultAttributes(variant=WINDOW_VARIANT_NORMAL)




      :param `variant`: 
      :type `variant`: wx.WindowVariant




      :rtype: :ref:`wx.VisualAttributes`








   .. method:: GetCurrentlySelectedFilename(self)

      Returns the path of the file currently selected in dialog.                  

      Notice that this file is not necessarily going to be accepted by the user, so calling this function mostly makes sense from an update UI event handler of a custom file dialog extra control to update its state depending on the currently selected file. 

      Currently this function is fully implemented under GTK and MSW and always returns an empty string elsewhere. 

                

      :rtype: `string`







      :returns: 

         The path of the currently selected file or an empty string if nothing is selected.  







      .. versionadded:: 2.9.5 
    







      .. seealso:: :meth:`SetExtraControlCreator`     








   .. method:: GetCurrentlySelectedFilterIndex(self)

      Returns the file type filter index currently selected in dialog.                  

      Notice that this file type filter is not necessarily going to be the one finally accepted by the user, so calling this function mostly makes sense from an update UI event handler of a custom file dialog extra control to update its state depending on the currently selected file type filter. 

      Currently this function is fully implemented under macOS and MSW and always returns  ``NOT_FOUND``   elsewhere. 

                

      :rtype: `int`







      :returns: 

         The 0-based index of the currently selected file type filter or ``wx.NOT_FOUND`` if nothing is selected.  







      .. versionadded:: 4.1/wxWidgets-3.1.3  
         - MSW   







      .. versionadded:: 4.1/wxWidgets-3.1.5  
         - macOS  







      .. seealso:: :meth:`SetExtraControlCreator`     







      .. seealso:: :meth:`GetFilterIndex`     







      .. seealso:: :meth:`SetFilterIndex`     








   .. 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:`wx.Window`







      .. versionadded:: 2.9.0 
     








   .. method:: GetFilename(self)

      Returns the default filename.                  

                

      :rtype: `string`







      .. note:: 

         This function can't be used with dialogs which have the  ``FD_MULTIPLE``   style, use  :meth:`GetFilenames`   instead.   








   .. method:: GetFilenames(self)

                      Returns a list of filenames chosen in the dialog.  This function
                      should only be used with the dialogs which have wx.``MULTIPLE`` style,
                      use GetFilename for the others. 

      :rtype: `list of strings`








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







      .. note:: 

         This function can't be used with dialogs which have the  ``FD_MULTIPLE``   style, use  :meth:`GetPaths`   instead.   








   .. method:: GetPaths(self)

                      Returns a list of the full paths of the files chosen. This function
                      should only be used with the dialogs which have wx.``MULTIPLE`` style, use
                      GetPath for the others.
                 

      :rtype: `list of strings`








   .. method:: GetWildcard(self)

      Returns the file dialog wildcard.                  

      :rtype: `string`








   .. method:: SetCustomizeHook(self, customizeHook)

      Set the hook to be used for customizing the dialog contents.                  

      This function can be called before calling :meth:`ShowModal`   to specify that the dialog contents should be customized using the provided hook. See :ref:`wx.FileDialogCustomizeHook`  documentation and :ref:`Dialogs Sample <dialogs sample>`  for the examples of using it. 




      :param `customizeHook`: The hook object that will be used by the dialog. This object must remain valid at least until :meth:`ShowModal`   returns.  
      :type `customizeHook`: wx.FileDialogCustomizeHook






      :rtype: `bool`



                  



      :returns: 

         ``True`` if the hook was successfully set or ``False`` if customizing the file dialog is not supported by the current platform.  







      .. versionadded:: 4.1/wxWidgets-3.1.7  
     







      .. note:: 

         In order to define a custom hook object,  :ref:`/filedlgcustomize.h`  must be included in addition to the usual   :ref:`/filedlg.h`  header.  








   .. 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 ``wx.OK``, and   ``ID_CANCEL``   otherwise.                   

      :rtype: `int`








   .. attribute:: CurrentlySelectedFilename

      See :meth:`~wx.FileDialog.GetCurrentlySelectedFilename`


   .. attribute:: CurrentlySelectedFilterIndex

      See :meth:`~wx.FileDialog.GetCurrentlySelectedFilterIndex`


   .. attribute:: Directory

      See :meth:`~wx.FileDialog.GetDirectory` and :meth:`~wx.FileDialog.SetDirectory`


   .. attribute:: ExtraControl

      See :meth:`~wx.FileDialog.GetExtraControl`


   .. attribute:: Filename

      See :meth:`~wx.FileDialog.GetFilename` and :meth:`~wx.FileDialog.SetFilename`


   .. attribute:: Filenames

      See :meth:`~wx.FileDialog.GetFilenames`


   .. attribute:: FilterIndex

      See :meth:`~wx.FileDialog.GetFilterIndex` and :meth:`~wx.FileDialog.SetFilterIndex`


   .. attribute:: Message

      See :meth:`~wx.FileDialog.GetMessage` and :meth:`~wx.FileDialog.SetMessage`


   .. attribute:: Path

      See :meth:`~wx.FileDialog.GetPath` and :meth:`~wx.FileDialog.SetPath`


   .. attribute:: Paths

      See :meth:`~wx.FileDialog.GetPaths`


   .. attribute:: Wildcard

      See :meth:`~wx.FileDialog.GetWildcard` and :meth:`~wx.FileDialog.SetWildcard`

