FileChooserDialog

A FileChooserDialog provides a FileChooserWidget within a dialog window.

The dialog-variant of the FileChooser allows for selecting of files and folders.

Constructor

The FileChooserDialog can be constructed using the following:

filechooserdialog = Gtk.FileChooserDialog(action, buttons)

The action parameter should be set to either Gtk.FileChooserAction.OPEN which allows opening of files, Gtk.FileChooserAction.SAVE which allows files to be saved, Gtk.FileChooserAction.SELECT_FOLDER which enables selecting of folders and Gtk.FileChooserAction.CREATE_FOLDER which creates folders based on a specified name. The buttons pareameter is a tuple of buttons and response_id values which will be displayed in the dialog.

Methods

Once the FileChooserDialog has been constructed use:

filechooserdialog.run()
filechooserdialog.destroy()

Note

If your application only uses a FileChooserDialog, the Gtk.main() call is not required. This is invoked automatically when calling filechooserdialog.run().

The title of the dialog which is displayed can be set with:

filechooserdialog.set_title(title)

When using a FileChooserDialog, the parent window should be defined. This ensures correct positioning of the dialog when it appears:

filechooserdialog.set_parent(parent)

Generally, the parent parameter will be a Window or some other dialog type.

Example

Below is an example of a FileChooserDialog:

#!/usr/bin/env python3

from gi.repository import Gtk

filechooserdialog = Gtk.FileChooserDialog()
filechooserdialog.set_title("FileChooserDialog")
filechooserdialog.add_button("_Open", Gtk.ResponseType.OK)
filechooserdialog.add_button("_Cancel", Gtk.ResponseType.CANCEL)
filechooserdialog.set_default_response(Gtk.ResponseType.OK)

response = filechooserdialog.run()

if response == Gtk.ResponseType.OK:
    print("File selected: %s" % filechooserdialog.get_filename())

filechooserdialog.destroy()

Download: FileChooserDialog