16.6. File Selections using FileChooser-based Widgets

The new way to select files in PyGTK 2.4 is to use the variants of the FileChooser widget. The two objects that implement this new interface in PyGTK 2.4 are FileChooserWidget and FileChooserDialog. The latter is the complete dialog with the window and easily defined buttons. The former is a widget useful for embedding within another widget.

Both the FileChooserWidget and FileChooserDialog possess the means for navigating the filesystem tree and selecting files. The view of the widgets depends on the action used to open a widget.

To create a new file chooser dialog to select an existing file (as in typical File->Open option of a typical application), use:

  chooser = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_OPEN,
                                  buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))

To create a new file chooser dialog to select a new file name (as in the typical File->Save as option of a typical application), use:

  chooser = gtk.FileChooserDialog(title=None,action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                  buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))

In the above examples, the two buttons (the stock Cancel and Open items) are created and connected to their respective responses (stock Cancel and OK responses).

To set the folder displayed in the file chooser, use the method:

  chooser.set_current_folder(pathname)

To set the suggested file name as if it was typed by a user (the typical File->Save Assituation), use the method:

  chooser.set_current_name(name)

The above method does not require the filename to exist. If you want to preselect a particular existing file (as in the File->Open situation), you should use the method:

  chooser.set_filename(filename)

To obtain the filename that the user has entered or clicked on, use this method:

  filename = chooser.get_filename()

It is possible to allow multiple file selections (only for the gtk.FILE_CHOOSER_ACTION_OPEN action) by using the method:

  chooser.set_select_multiple(select_multiple)

where select_mutiple should be TRUE to allow multiple selections. In this case, you will need to use the following method to retrieve a list of the selected filenames:

  filenames = chooser.get_filenames()

An important feature of all file choosers is the ability to add file selection filters. The filter may be added by the method:

  chooser.add_filter(filter)

In the example above, filter must be an instance of the FileFilterclass.

The left panel of the file chooser lists some shortcut folders such as Home, Filesystem, CDROM, etc. You may add a folder to the list of these shortcuts and remove it from the list by using these methods:

  chooser.add_shortcut_folder(folder)
  chooser.remove_shortcut_folder(folder)

where folder is the pathname of folder. The filechooser.py example program illustrates the use of the filechooser widget. Figure 16.12, “File Selection Example” shows the resulting display:

Figure 16.12. File Selection Example

File Selection Example

The source code for the filechooser.py example program is:

    1   #!/usr/bin/env python
    2
    3   # example filechooser.py
    4
    5   import pygtk
    6   pygtk.require('2.0')
    7
    8   import gtk
    9
   10   # Check for new pygtk: this is new class in PyGtk 2.4
   11   if gtk.pygtk_version < (2,3,90):
   12      print "PyGtk 2.3.90 or later required for this example"
   13      raise SystemExit
   14
   15   dialog = gtk.FileChooserDialog("Open..",
   16                                  None,
   17                                  gtk.FILE_CHOOSER_ACTION_OPEN,
   18                                  (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
   19                                   gtk.STOCK_OPEN, gtk.RESPONSE_OK))
   20   dialog.set_default_response(gtk.RESPONSE_OK)
   21
   22   filter = gtk.FileFilter()
   23   filter.set_name("All files")
   24   filter.add_pattern("*")
   25   dialog.add_filter(filter)
   26
   27   filter = gtk.FileFilter()
   28   filter.set_name("Images")
   29   filter.add_mime_type("image/png")
   30   filter.add_mime_type("image/jpeg")
   31   filter.add_mime_type("image/gif")
   32   filter.add_pattern("*.png")
   33   filter.add_pattern("*.jpg")
   34   filter.add_pattern("*.gif")
   35   filter.add_pattern("*.tif")
   36   filter.add_pattern("*.xpm")
   37   dialog.add_filter(filter)
   38
   39   response = dialog.run()
   40   if response == gtk.RESPONSE_OK:
   41       print dialog.get_filename(), 'selected'
   42   elif response == gtk.RESPONSE_CANCEL:
   43       print 'Closed, no files selected'
   44   dialog.destroy()