How do I use standard file dialogs

Most GUI applications must save and load data of some kind or another. For the sanity of both you and your users, having a single, consistent mechanism for choosing files is desirable. Happily, wxPython provides the standard dialog wx.FileDialog to insert into your applications for this purpose. Under MS Windows, this class is a wrapper around the standard Windows file dialog. Under an X Window system, this is a similar looking custom dialog. Figure 6.5 displays the file dialog for the sketch application.

Figure 6.5 A standard file dialog for Windows

The most important method for using the wx.FileDialog is the constructor, which has the following signature.

wx.FileDialog(parent, message="Choose a file", defaultDir="", defaultFile="", wildcard="*.*", style=0)

Table 6.6 describes the parameters of the constructor.

Table 6.6 The parameters of the wx.FileDialog constructor




The parent window for the dialog, or None if there is no parent window.


The message is displayed in the title bar of the dialog.


The directory that the dialog should start with. If empty, the dialog starts in the current working directory.


The file selected when the dialog opens. If empty, no file is selected.


The options for the wildcard filter which allows the user to limit the display to selected file types. The format is <display>|<wildcard>, which may be repeated multiple times to give the user multiple options; for example, "Sketch files (*.sketch)|*.sketch|All files (* *)|* *"


A bitmask style. Styles are listed in table 6.6.

Table 6.7 lists the style options for the style bitmask.

Wxpython Filedialog Select Folder
Table 6.7 Style options for wx.FileDialog




After the user selects a file, the current working directory is changed to that directory.


Only applicable for an open dialog, this style allows the user to select multiple files.


The style is used for opening a file.


Only applicable for a save dialog, this style gives a prompt to confirm the choice if an existing file is selected to be overwritten.


The style is used for saving a file.

To use the file dialog, call the ShowModal() method on a dialog instance. The method returns either wx.ID_0K or wx.ID_CANCEL, depending on the button the user clicks to dismiss the dialog. After the selection, use the GetFilename(), Get-Directory(), or GetPath() methods to retrieve the data. Afterwards, it's a good idea to destroy the dialog with the Destroy() method.

Listing 6.6 displays the modifications necessary to the SketchFrame to support saving and loading. These changes also require the import of the cPickle and os standard modules. We'll use cPickle to convert the list of data from the sketch window to a format that can be written to, and read from, the file.

Listing 6.6 Saving and loading methods of SketchFrame def _init_(self, parent):

self.title = "Sketch Frame"

wx.Frame._init_(self, parent, -1, self.title, size=(800,600)) self.filename = ""

self.sketch = SketchWindow(self, -1)

self.sketch.Bind(wx.EVT_MOTION, self.OnSketchMotion) self.initStatusBar() self.createMenuBar() self.createToolBar()

def SaveFile(self):

if self.filename:

data = self.sketch.GetLinesData() f = open(self.filename, 'w') cPickle.dump(data, f) f.close()

Q Saving the file def ReadFile(self): C Reading the file if self.filename: try:

f = open(self.filename, 'r') data = cPickle.load(f) f.close()

self.sketch.SetLinesData(data) except cPickle.UnpicklingError:

wx.MessageBox("%s is not a sketch file." % self.filename, "oops!", style=wx.OK|wx.ICON_EXCLAMATION)

wildcard = "Sketch files (*.sketch)|*.sketch|All files (*.*)|*.*"

def OnOpen(self, event):

dlg = wx.FileDialog(self, "Open sketch file...", os.getcwd(), style=wx.OPEN, wildcard=self.wildcard) if dlg.ShowModal() == wx.ID_OK: self.filename = dlg.GetPath() self.ReadFile()

self.SetTitle(self.title + ' -- ' + self.filename) dlg.Destroy()

Popping up the d open dialog def OnSave(self, event): Q Saving the file if not self.filename:

self.OnSaveAs(event) else:


def OnSaveAs(self, event):

dlg = wx. FileDialog(self, "Save sketch as...", f Popping up the save dialog os.getcwd(), style=wx.SAVE | wx.OVERWRITE_PROMPT, wildcard=self.wildcard) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetPath()

if not os.path.splitext(filename)[1]: g Ensuring filename extensions filename = filename + '.sketch' self.filename = filename self.SaveFile()

self.SetTitle(self.title + ' -- ' + self.filename) dlg.Destroy()

O This method writes the file data to disk, given the filename, and uses the cPickle module.

C This method reads the file using cPickle. If the file is not of the expected type, it pops up a message box alert to that effect.

d The OnOpen() method creates a dialog with the wx.OPEN style, in the current directory. The wildcard string on the previous line allows the user to limit the selection to .sketch files. If the user clicks OK, this method calls the ReadFile() method with the selected path.

Q If a filename has already been selected for the current data, we save the file, otherwise, we treat it as a save as, and open the save dialog.

Q The OnSave() method creates a wx.SAVE file dialog.

® This line ensures that filenames typed without an extension get the .sketch extension.

In the next section, we'll describe how to use the color picker.

Was this article helpful?

0 0


    How to use wildcards FileDialog wxPython?
    2 years ago

Post a comment