How can I use a file picker

File-choosing dialogs tend to be consistent from application to application. In wxPython, the wx.FileDialog uses the native OS dialog for the major platforms, and uses non-native look-alikes for other operating systems. The MS Windows version is displayed in figure 9.6.

Wxpython File Dialog

Figure 9.6

The standard Windows file chooser

Figure 9.6

The standard Windows file chooser

You can set up the file dialog to start in any directory, and you can also pass it a wildcard filter to limit the display to only certain file types. Listing 9.6 displays a basic example.

Listing 9.6 Using wx.FileDialog import wx import os if _name_ == "_main_":

app = wx.PySimpleApp()

wildcard = "Python source (*.py)|*.py|" \ "Compiled Python (*.pyc)|*.pyc|" \ "All files (*.*)|*.*" dialog = wx.FileDialog(None, "Choose a file", os.getcwd(),

"", wildcard, wx.OPEN) if dialog.ShowModal() == wx.ID_OK:

print dialog.GetPath() dialog.Destroy()

The file dialog is the most complex dialog we've seen in this chapter, in that it has several properties that can be programmatically read and written. The constructor allows you to set some of the properties, as in:

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

The message argument appears in the title bar of the window. The defaultDir tells the dialog what directory to display initially. If the argument is empty or represents a directory that doesn't exist, the dialog starts in the current working directory. The defaultFile preselects a file, typically used when saving a file. The wildcard argument allows you to filter the list based on a given pattern, using the usual * and ? as wildcard characters. The wildcard can either be a single pattern, such as *.py, or a set of patterns in the format <description> | <pattern> | <description> | <pattern>—similar to the wildcard used in listing 9.6.

"Python source (*.py)|*.py|Compiled Python (*.pyc)|*.pyc| All files (*.*)|*.*"

If there is a pattern with multiple entries, they display in the familiar pull-down menu shown in figure 9.6. The pos argument is not guaranteed to be supported by the underlying system.

Selecting a file

The two most important style flags for wx.FileDialog are wx.open and wx.save, which indicate the kind of dialog and affect the behavior of the dialog.

A dialog used for opening a file has two flags that further affect behavior. The flag wx.hide_readonly causes the dialog to gray out the checkbox that allows the user to open the file in read-only mode. The flag wx.multiple allows the user to select multiple files in a single directory for opening.

Save file dialogs have one useful flag, wx.overwrite_prompt, that forces the user to confirm saving a file if the file already exists.

Either kind of file dialog can use the wx.change_dir flag. When this flag is raised, a file selection also changes the application's working directory to the directory where the selection took place. Among other things, this allows the next file dialog to open in the same directory without requiring that the application store that value elsewhere.

Unlike the other dialogs we've seen so far in this chapter, these properties are all gettable and settable via methods. This is true for the properties directory, filename, style, message, and wildcard, all of which have getters and setters using the usual Get/Set naming convention.

After the user has exited the dialog, and after checking that it was exited with wx.ok, you can get the user's choice by using the method GetPath(), which returns the full pathname of the file as a string. If the dialog is an open dialog with wx.multiple selected, use GetPaths() instead. That method returns a Python list of path strings. If for some reason you need to know which of the pull-down filters was active when the user made her selection, you can use the GetFilterindex() method, which returns an integer index into the list. To change the index pro-grammatically, use SetFilterindex().

The following is a convenience function for using file dialogs.

wx.FileSelector(message, default_path="", default_filename="", default_extension="", wildcard="*.*'', flags=0, parent=None, x=-1, y=-1)

The message, default_path, default_filename, and wildcard arguments do what you'd expect from the constructor, despite being named differently. The flags argument is normally called style, and the default_extension adds an extension onto a selected file name that doesn't when you save a file. The return value is the string pathname if the user presses OK, or an empty string if the user presses Cancel.

Selecting a directory

If the user wants to select a directory rather than a file, use wx.DirDialog, which presents a tree view of the directory structure as shown in figure 9.7.

The directory selector is somewhat simpler than a file dialog. Listing 9.7 displays the relevant code.

Listing 9.7 Displaying a directory chooser dialog import wx if __name__ == "__main__": app = wx.PySimpleApp()

dialog = wx.DirDialog(None, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON) if dialog.ShowModal() == wx.ID_OK:

print dialog.GetPath() dialog.Destroy()

Browse For Folder (T|S

Choose a directory:

O Desktop E Q My Documents S) 9 jMy Computer ]

ED '■Q My Network Places a New Folder

Figure 9.7

A directory selection dialog

Nearly all of the functionality of this dialog is in the constructor.

wx.DirDialog(parent, message="Choose a directory", defaultPath="", style=0, pos = wx.DefaultPosition, size = wx.DefaultSize, name="wxDirCtrl")

Because the message argument displays inside the dialog itself, you don't need a hook to change the title bar. The defaultPath tells the dialog what to select, and if it's empty, the dialog shows the root of the file system. The pos and size arguments are ignored under MS Windows, and the name argument is ignored in all operating systems. The style flag for this dialog, wx.dd_new_dir_button, gives the dialog a button for creating a directory. This flag may not work in older versions of MS Windows.

The path, message, and style properties of this class have typical getters and setters. You can use the GetPath() method to retrieve the user selection after the dialog is dispatched. This dialog also has a convenience function.

wx.DirSelector(message=wx.DirSelectorPromptStr, default_path="", style=0, pos=wxDefaultPosition, parent=None)

All arguments are as in the constructor. The function returns the selected directory name as a string if OK is pressed, and the empty string if Cancel is pressed.

Was this article helpful?

+5 -2

Responses

  • monika
    How to open and save a files by using wx.FileDialog?
    7 years ago
  • steffen
    How to store multiple objects in clipboard in wxpython?
    7 years ago
  • valdemar
    Why compile Python .py files to .pyc wxpython?
    6 years ago
  • JOHANNES
    How do i look for wildcard file in python?
    3 years ago

Post a comment