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
This class supports the following styles:
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 ”.bmp”, and both “BMP files (.bmp)|.bmp|GIF files (.gif)|.gif” and “Image files|.bmp;.gif” are errors.
See also
__init__ | Constructor. |
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 |
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 |
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)
Constructor.
Use ShowModal to show the dialog.
Parameters: |
|
---|
Returns the default directory.
Return type: | string |
---|
If functions SetExtraControlCreator and ShowModal were called, returns the extra window.
Otherwise returns None.
Return type: | Window |
---|
New in version 2.9.0.
Returns the default filename.
Return type: | string |
---|
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 |
---|
Returns the message that will be displayed on the dialog.
Return type: | string |
---|
Returns the full path (directory and filename) of the selected file.
Return type: | string |
---|
Returns the file dialog wildcard.
Return type: | string |
---|
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.
Return type: | int |
---|
See GetDirectory and SetDirectory
See GetExtraControl
See GetFilename and SetFilename
See GetFilenames
See GetFilterIndex and SetFilterIndex
See GetMessage and SetMessage
See GetPaths
See GetWildcard and SetWildcard