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
This class supports the following styles:
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.
|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.|
|Directory||See GetDirectory and SetDirectory|
|Filename||See GetFilename and SetFilename|
|FilterIndex||See GetFilterIndex and SetFilterIndex|
|Message||See GetMessage and SetMessage|
|Path||See GetPath and SetPath|
|Wildcard||See GetWildcard and SetWildcard|
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.
Use ShowModal to show the dialog.
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.
|Returns:||The path of the currently selected file or an empty string if nothing is selected.|
New in version 2.9.5.
Returns the default directory.
If functions SetExtraControlCreator and ShowModal were called, returns the extra window.
Otherwise returns None.
New in version 2.9.0.
Returns the default filename.
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.
Returns the message that will be displayed on the dialog.
Returns the full path (directory and filename) of the selected file.
Returns the file dialog wildcard.
Sets the default directory.
|Parameters:||directory (string) –|
Sets the default filename.
In wxGTK this will have little effect unless a default directory has previously been set.
|Parameters:||setfilename (string) –|
Sets the default filter index, starting from zero.
|Parameters:||filterIndex (int) –|
Sets the message that will be displayed on the dialog.
|Parameters:||message (string) –|
Sets the path (the combined directory and filename that will be returned when the dialog is dismissed).
|Parameters:||path (string) –|
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) –|
Shows the dialog, returning ID_OK if the user pressed OK, and ID_CANCEL otherwise.