FileChooserDialog (Python) Marta Maria Casetti mmcasetti@gmail.com 2012 A dialog suitable for "Open" and "Save" commands FileChooserDialog

This FileChooserDialog saves a text document, which can be opened or written from scratch in a TextView (see below).

It is also possible to call a FileChooserDialog to open a new document.

Steps to recreate the example

Create a file .ui to describe an app-menu with items "New", "Open", "Save", "Save as", and "Quit". This can be done with Glade or in a text editor. See

Create a Python program for a Gtk.TextView with a Gtk.Buffer self.buffer, and a self.file which will be a Gio.File and we set initially as None.

In this program, create also the actions corresponding to the items in the app-menu, connect them to callback functions, and import the menu in the do_startup() method with a Gtk.Builder.

"New" and "Quit" actions and callback functions are quite straightforward, see . See for a more detailed explanation of signals and callback functions.

"Open" callback should create and open a Gtk.FileChooserDialog for "Open", connected with another callback function for each of the two "Open" and "Cancel" buttons of the FileChooserDialog.

"Save as" works basically as "Open", but the callback function of the "Save" button depends on a more complex method save_to_file().

"Save" can be reduced to the case where the file is None, that is the case where self.file is a new file, which in turn is the case "Save as"; and to the case where the file is not None, which in turn is reduced to save_to_file().

Finally, the method save_to_file(): see , lines 146 - 175.

XML file which creates the app-menu
Code used to generate this example
Useful methods for a FileChooserDialog

Note that the action of the FileChooserDialog can be one of the following: Gtk.FileChooserAction.OPEN (the file chooser will only let the user pick an existing file) Gtk.FileChooserAction.SAVE (the file chooser will let the user pick an existing file, or type in a new filename), Gtk.FileChooserAction.SELECT_FOLDER (the file chooser will let the user pick an existing folder), Gtk.FileChooserAction.CREATE_FOLDER (the file chooser will let the user name an existing or new folder).

Besides the methods used in the , we have:

set_show_hidden(True) is used to display hidden files and folders.

set_select_multiple(True) sets that multiple files can be selected. This is only relevant if the mode is Gtk.FileChooserAction.OPEN or Gtk.FileChooserAction.SELECT_FOLDER.

In a "Save as" dialog, set_current_name(current_name) sets current_name in the file selector, as if entered by the user; current_name can be something like Untitled.txt. This method should not be used except in a "Save as" dialog.

The default current folder is "recent items". To set another folder use set_current_folder_uri(uri); but note you should use this method and cause the file chooser to show a specific folder only when you are doing a "Save as" command and you already have a file saved somewhere.

API References

In this sample we used the following:

GtkFileChooserDialog

GtkFileChooser

GtkWindow

GtkTextView

GtkTextBuffer

GtkScrolledWindow

GFile

GSimpleAction

GtkBuilder