File Dialog

Find Dialog Sample

Creating the Dialog

Create a File Dialog by calling dialog.fileDialog with the arguments shown in the following table.

Argument
Data type/notes
self
the window (background) that is the parent for the dialog
message
quoted string defining the title for the dialog

path

quoted string defining the default path to be used for the dialog

fileName

quoted string defining a file name to be used typically in saving a file

filter
quoted string containing the pattern to be used to identify files to be shown to the user by default when the File Dialog opens. The dialog above was created with a pattern of "*.py." A more complex example is shown below.

OPTIONAL style

A boolean logic expression containing one or more pre-defined window style constants that define how the window will look and behave.


Example:

The above dialog was created with this line of code:

 result = dialog.fileDialog(self, 'Open', '', '', wildcard )

after setting the variable wildcard to the string "*.py".

To open a File Dialog showing all JPEG and GIF files in the current directory, you would first set the wildcard string to "JPG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF files (*.gif)|*.gif|All Files (*.*)|*.*" and then use the above line to open the File Dialog.

The optional style argument in dialog.fileDialog is generally not used when opening a File dialog to save a document because its default settings -- wx.OPEN | wx.MULTIPLE -- is the generally accepted norm for such dialogs. In creating dialogs for saving files, on the other hand, it is often useful to define a variable (here called "aStyle") which defines more fully how the save process will be managed. For example:

aStyle = wx.SAVE | wx.HIDE_READONLY | wx.OVERWRITE_PROMPT

will create a file save dialog (rather than the default file open), in which read-only files are not shown and an attempt to overwrite an existing file is confirmed with a prompt dialog.

Interacting With the Dialog

The fileDialog component returns a wxPython DialogResults object which contains 2 attributes, as shown in the following table

Name of value
Description
accepted
True = user clicked OK
False = user clicked Cancel
paths
list of strings containing the full pathnames to all files selected by the user

Example:

The sample dialog shown at the top of this page returns the following results:

accepted: True
paths: ['C:\\pycode\\PythonCard\\setup.py']