Gtk.FileChooserDialog
Gtk.FileChooserDialog — A file chooser dialog, suitable for “File/Open” or “File/Save” commands
Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Container
╰── Gtk.Bin
╰── Gtk.Window
╰── Gtk.Dialog
╰── Gtk.FileChooserDialog
See also:
Functions:
- new
() -> Gtk.Widget
Description:
Gtk.FileChooserDialog
is a dialog box suitable for use with File/Open
or File/Save as
commands.
This widget works by putting a Gtk.FileChooserWidget inside a GtkDialog. It exposes the Gtk.FileChooser interface, so you can use all of the Gtk.FileChooser functions on the file chooser dialog as well as those for Gtk.Dialog.
Note that Gtk.FileChooserDialog
does not have any methods of its own. Instead, you should use the functions that work on a Gtk.FileChooser.
Typical usage
In the simplest of cases, you can the following code to use Gtk.FileChooserDialog
to select a file for opening:
def on_open(self, event):
dialog = Gtk.FileChooserDialog("Please choose a file", self.win,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
self.win.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
file_path = dialog.get_filename()
print("Open clicked")
print("File selected: " + file_path)
#self.labelframe.set_label(os.path.basename(file_path))
#self.editor.open_file(file_path)
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
To use a dialog for saving, you can use this:
def on_save(self, event):
print('save')
dialog = Gtk.FileChooserDialog("Please choose a file", self.win,
Gtk.FileChooserAction.SAVE,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.OK))
self.win.add_filters(dialog)
response = dialog.run()
if response == Gtk.ResponseType.OK:
file_path = dialog.get_filename()
print("Save clicked")
print("File selected: " + file_path)
#self.labelframe.set_label(os.path.basename(file_path))
#self.editor.save_file(file_path)
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()
Setting up a file chooser dialog
There are various cases in which you may need to use a Gtk.FileChooserDialog
:
To select a file for opening. Use
Gtk.FileChooserAction.OPEN
.To save a file for the first time. Use
Gtk.FileChooserAction.SAVE
, and suggest a name such asUntitled
withGtk.FileChooser::set_current_name()
.To save a file under a different name. Use
Gtk.FileChooserAction.SAVE
, and set the existing filename withGtk.FileChooser::set_filename()
.To choose a folder instead of a file. Use
Gtk.FileChooserAction.SELECT_FOLDER
.
Note that old versions of the file chooser’s documentation suggested using Gtk.FileChooser::set_current_folder()
in various situations, with the intention of letting the application suggest a reasonable default folder. This is no longer considered to be a good policy, as now the file chooser is able to make good suggestions on its own. In general, you should only cause the file chooser to show a specific folder when it is appropriate to use Gtk.FileChooser::set_filename()
, i.e. when you are doing a Save As command and you already have a file saved somewhere.
Response Codes
Gtk.FileChooserDialog
inherits from Gtk.Dialog
, so buttons that go in its action area have response codes such as Gtk.ResponseType.ACCEPT
and Gtk.ResponseType.CANCEL
. For example, you could call Gtk.FileChooserDialog::new()
as follows:
dialog = Gtk.FileChooserDialog("Open File",
parent_window,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
)
This will create buttons for Cancel
and Open
that use stock response identifiers from GtkResponseType. For most dialog boxes you can use your own custom response codes rather than the ones in GtkResponseType, but Gtk.FileChooserDialog
assumes that its accept
-type action, e.g. an Open
or Save
button, will have one of the following response codes:
- Gtk.ResponseType.ACCEPT
- Gtk.ResponseType.OK
- Gtk.ResponseType.YES
- Gtk.ResponseType.APPLY
This is because Gtk.FileChooserDialog
must intercept responses and switch to folders if appropriate, rather than letting the dialog terminate — the implementation uses these known response codes to know which responses can be blocked if appropriate.
To summarize, make sure you use a [stock response code][gtkfilechooserdialog-responses] when you use Gtk.FileChooserDialog
to ensure proper operation.
Function Details:
new()
new () -> Gtk.Widget
Creates a new Gtk.FileChooserDialog
. This function is analogous to`Gtk.Dialog:new_with_buttons
()`.
Returns: a new Gtk.FileChooserDialog
Since: 2.4
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
filechooserdialog = Gtk.FileChooserDialog("Open...",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
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()