Table Of Contents

Previous topic

FileDataObject

Next topic

FileDirPickerEvent

This Page

Phoenix Logo

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

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 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 “x.bmp”, and both “BMP files (.bmp)|.bmp|GIF files (.gif)|.gif” and “Image files|.bmp;.gif” are errors.


class_hierarchy Class Hierarchy

Inheritance diagram for class FileDialog:


method_summary Methods Summary

__init__ Constructor.
GetCurrentlySelectedFilename Returns the path of the file currently selected in dialog.
GetDirectory Returns the default directory.
GetExtraControl If functions SetExtraControlCreator and ShowModal were called, returns the extra window.
GetFilename Returns the default filename.
GetFilterIndex Returns the index into the list of filters supplied, optionally, in the wildcard parameter.
GetMessage Returns the message that will be displayed on the dialog.
GetPath Returns the full path (directory and filename) of the selected file.
GetWildcard Returns the file dialog wildcard.
SetDirectory Sets the default directory.
SetFilename Sets the default filename.
SetFilterIndex Sets the default filter index, starting from zero.
SetMessage Sets the message that will be displayed on the dialog.
SetPath Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).
SetWildcard Sets the wildcard, which can contain multiple file types, for example: “BMP files (.bmp)|.bmp|GIF files (.gif)|.gif”.
ShowModal Shows the dialog, returning ID_OK if the user pressed OK, and ID_CANCEL otherwise.

api Class API



class FileDialog(Dialog)

Possible constructors:

FileDialog(parent, message=FileSelectorPromptStr,
           defaultDir="", defaultFile="",
           wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE,
           pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr)

This class represents the file chooser dialog.


Methods



__init__(self, parent, message=FileSelectorPromptStr, defaultDir="", defaultFile="", wildcard=FileSelectorDefaultWildcardStr, style=FD_DEFAULT_STYLE, pos=DefaultPosition, size=DefaultSize, name=FileDialogNameStr)

Constructor.

Use ShowModal to show the dialog.

Parameters:
  • parent (Window) – Parent window.
  • message (string) – Message to show on the dialog.
  • defaultDir (string) – The default directory, or the empty string.
  • defaultFile (string) – The default filename, or the empty string.
  • wildcard (string) – 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.
  • style (long) – A dialog style. See FD_* styles for more info.
  • pos (Point) – Dialog position. Not implemented.
  • size (Size) – Dialog size. Not implemented.
  • name (string) – Dialog name. Not implemented.


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.

Return type:string
Returns:The path of the currently selected file or an empty string if nothing is selected.

New in version 2.9.5.

See also

SetExtraControlCreator



GetDirectory(self)

Returns the default directory.

Return type:string


GetExtraControl(self)

If functions SetExtraControlCreator and ShowModal were called, returns the extra window.

Otherwise returns None.

Return type: Window

New in version 2.9.0.



GetFilename(self)

Returns the default filename.

Return type:string


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.

Return type:int


GetMessage(self)

Returns the message that will be displayed on the dialog.

Return type:string


GetPath(self)

Returns the full path (directory and filename) of the selected file.

Return type:string


GetWildcard(self)

Returns the file dialog wildcard.

Return type:string


SetDirectory(self, directory)

Sets the default directory.

Parameters:directory (string) –


SetFilename(self, setfilename)

Sets the default filename.

In wxGTK this will have little effect unless a default directory has previously been set.

Parameters:setfilename (string) –


SetFilterIndex(self, filterIndex)

Sets the default filter index, starting from zero.

Parameters:filterIndex (int) –


SetMessage(self, message)

Sets the message that will be displayed on the dialog.

Parameters:message (string) –


SetPath(self, path)

Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).

Parameters:path (string) –


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.

Parameters:wildCard (string) –


ShowModal(self)

Shows the dialog, returning ID_OK if the user pressed OK, and ID_CANCEL otherwise.

Return type:int

Properties



CurrentlySelectedFilename

See GetCurrentlySelectedFilename



Directory

See GetDirectory and SetDirectory



ExtraControl

See GetExtraControl



Filename

See GetFilename and SetFilename



Filenames

See GetFilenames



FilterIndex

See GetFilterIndex and SetFilterIndex



Message

See GetMessage and SetMessage



Path

See GetPath and SetPath



Paths

See GetPaths



Wildcard

See GetWildcard and SetWildcard