gtk.FileChooser — an interface for choosing files used by gtk.FileChooserWidget and gtk.FileChooserDialog (new in PyGTK 2.4)
|
"confirm-overwrite" | def callback(filechooser, user_param1, ...) |
"current-folder-changed" | def callback(filechooser, user_param1, ...) |
"file-activated" | def callback(filechooser, user_param1, ...) |
"selection-changed" | def callback(filechooser, user_param1, ...) |
"update-preview" | def callback(filechooser, user_param1, ...) |
This interface is available in PyGTK 2.4 and above.
gtk.FileChooser is an interface that can be implemented by file selection widgets. In PyGTK, the main objects that implement this interface are gtk.FileChooserWidget and gtk.FileChooserDialog. You do not need to write an object that implements the gtk.FileChooser interface unless you are trying to adapt an existing file selector to expose a standard programming interface.
gtk.FileChooser has several elements to its display:
a list of shortcut folders on the left that is divided into two lists by a horizontal line:
You can add a custom preview widget to a file chooser and get notification when the preview needs to be updated. To install a preview widget, use the set_preview_widget() method. Then, connect to the "update-preview" signal to be notified when you need to update the contents of the preview.
Your callback should use the get_preview_filename() method to see what needs previewing. Once you have generated the preview for the corresponding file, you must call the set_preview_widget_active() method with a boolean flag that indicates whether your callback could successfully generate a preview. An example use of a custom preview is:
... preview = gtk.Image() my_file_chooser.set_preview_widget(preview) my_file_chooser.connect("update-preview", update_preview_cb, preview) ... def update_preview_cb(file_chooser, preview): filename = file_chooser.get_preview_filename() try: pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(filename, 128, 128) preview.set_from_pixbuf(pixbuf) have_preview = True except: have_preview = False file_chooser.set_preview_widget_active(have_preview) return ... |
You can add extra widgets to a file chooser to provide options that are not present in the default design. For example, you can add a gtk.ToggleButton to give the user the option to open a file in read-only mode. You can use the set_extra_widget() method to insert additional widgets in a file chooser. For example:
toggle = gtk.CheckButton("Open file read-only") toggle.show () my_file_chooser.set_extra_widget(toggle) |
If you want to set more than one extra widget in the file chooser, you can use a container such as a gtk.VBox or a gtk.Table to hold your widgets; then set the container as the whole extra widget.
The gtk.FileChooserDialog uses the private GtkFileChooserDefaultClass that has several key bindings and their associated signals. This section describes the available key binding signals.
The default keys that activate the key-binding signals in GtkFileChooserDefaultClass are as follows:
Signal name | Key |
---|---|
location-popup | Control-L |
up-folder | Alt-Up |
down-folder | Alt-Down |
home-folder | Alt-Home |
To change these defaults to something else, you could include the following fragment in your .gtkrc-2.0 file:
binding "my-own-gtkfilechooser-bindings" { bind "<Alt><Shift>l" { "location-popup" () } bind "<Alt><Shift>Up" { "up-folder" () } bind "<Alt><Shift>Down" { "down-folder" () } bind "<Alt><Shift>Home" { "home-folder-folder" () } } class "GtkFileChooserDefault" binding "my-own-gtkfilechooser-bindings" |
The "GtkFileChooserDefault::location-popup" signal is used to make the file chooser show a "Location" dialog which the user can use to manually type the name of the file he wishes to select. By default this is bound to Control-L.
def location_popup_cb(filechooser, user_data): |
where filechooser is the gtk.FileChooser that received the signal, user_data is user data set when the signal handler was connected.
The "GtkFileChooserDefault::up-folder" signal is used to make the file chooser go to the parent of the current folder in the file hierarchy. By default this is bound to Alt-Up.
def up_folder_cb(filechooser, user_data): |
where filechooser is the object that received the signal and user_data is the user data set when the signal handler was connected.
The "GtkFileChooserDefault::down-folder" signal is used to make the file chooser go to a child of the current folder in the file hierarchy. The subfolder that will be used is displayed in the path bar widget of the file chooser. For example, if the path bar is showing "/foo/bar/baz", then this will cause the file chooser to switch to the "baz" subfolder. By default this is bound to Alt-Down.
def down_folder_cb(filechooser, user_data): |
where filechooser is the object that received the signal and user_data is the user data set when the signal handler was connected.
The "GtkFileChooserDefault::home-folder" signal is used to make the file chooser show the user's home folder in the file list. By default this is bound to Alt-Home.
def home_folder_cb(filechooser, user_data): |
where filechooser is the object that received the signal and user_data is the user data set when the signal handler was connected.
|
action : | the file selection action - one of: gtk.FILE_CHOOSER_ACTION_OPEN, gtk.FILE_CHOOSER_ACTION_SAVE, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER or gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER. |
This method is available in PyGTK 2.4 and above.
The set_action() method sets the "action" property to the value of action. The type of operation that that the chooser is performing is set by action causing the user interface to be changed to suit the selected action. The value of action must be one of:
gtk.FILE_CHOOSER_ACTION_OPEN | Indicates open mode. The file chooser will only let the user pick an existing file. |
gtk.FILE_CHOOSER_ACTION_SAVE | Indicates save mode. The file chooser will let the user pick an existing file, or type in a new filename. |
gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER | Indicates an open mode for selecting folders. The file chooser will let the user pick an existing folder. |
gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER | Indicates a mode for creating a new folder. The file chooser will let the user name an existing or new folder |
For example, an option to create a new folder might be shown if the action is gtk.FILE_CHOOSER_ACTION_SAVE but not if the action is gtk.FILE_CHOOSER_ACTION_OPEN.
|
Returns : | the action that is set for the file selector |
This method is available in PyGTK 2.4 and above.
The get_action() method returns the value of the "action" property that indicates the type of operation that the file chooser is set to perform. See the set_action() method for more information.
|
local_only : | if TRUE, only local files can be selected |
This method is available in PyGTK 2.4 and above.
The set_local_only() method sets the "local-only" property to the value of local_only. If local_only is TRUE (the default), only local files can be selected in the file selector and the selected files are guaranteed to be accessible through the operating system's native file system. Therefore, the application only needs to use the filename methods in gtk.FileChooser. For example, the application can use the get_filename() method instead of the URI method get_uri().
|
Returns : | TRUE if only local files can be selected. |
This method is available in PyGTK 2.4 and above.
The get_local_only() method returns the value of the "local-only" property that indicates whether only local files can be selected in the file selector. See the set_local_only() method for more information.
|
select_multiple : | if TRUE multiple files can be selected. |
This method is available in PyGTK 2.4 and above.
The set_select_multiple() method sets the "select_multiple" property to the value of select_multiple. If select_multiple is TRUE, multiple files can be selected in the file selector.
The "select-multiple" property cannot be set TRUE when the file chooser action is gtk.FILE_CHOOSER_ACTION_SAVE or gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER.
|
Returns : | TRUE if multiple files can be selected. |
This method is available in PyGTK 2.4 and above.
The get_select_multiple() method returns the value of the "select_multiple" property that indicates whether multiple files can be selected in the file selector. See the set_select_multiple() method for more information.
|
name : | the filename to use, as a UTF-8 string |
This method is available in PyGTK 2.4 and above.
The set_current_name() method sets the current name in the file selector to the value of name, as if entered by the user. Note that the name passed in here is a UTF-8 string rather than a filename. This method is meant for such uses as a suggested name in a "Save As..." dialog.
If you want to preselect a particular existing file, you should use the set_filename() method instead.
|
Returns : | The currently selected filename, or None if no file is selected, or the selected file can't be represented with a local filename. |
This method is available in PyGTK 2.4 and above.
The get_filename() method returns the filename of the currently selected file in the file selector. If multiple files are selected, one of the filenames will be returned at random. If the file chooser is in folder mode, this function returns the selected folder.
|
filename : | the filename to set as current |
Returns : | TRUE if both the folder could be changed and the file was selected successfully, FALSE otherwise. |
This method is available in PyGTK 2.4 and above.
The set_filename() method sets filename as the current filename for the the file chooser. If the file name isn't in the current folder of the chooser, then the current folder of the chooser will be changed to the folder containing filename. This is equivalent to a sequence of unselect_all() followed by select_filename().
Note that the file must exist, or nothing will be done except for the directory change. To pre-enter a filename for the user, as in a "Save as ..." dialog, use the set_current_name() method.
|
filename : | the filename to select |
Returns : | TRUE if both the folder could be changed and the file was selected successfully, FALSE otherwise. |
This method is available in PyGTK 2.4 and above.
The select_filename() method selects the filename specified by filename. If filename isn't in the current folder of the chooser, then the current folder of the chooser will be changed to the folder containing filename.
|
filename : | the filename to unselect |
This method is available in PyGTK 2.4 and above.
The unselect_filename() method unselects the currently selected filename specified by filename. If filename is not in the current directory, does not exist, or is otherwise not currently selected, this method does nothing.
|
This method is available in PyGTK 2.4 and above.
The select_all() method selects all the files in the current folder of a file chooser.
|
This method is available in PyGTK 2.4 and above.
The unselect_all() method unselects all the files in the current folder of a file chooser.
|
Returns : | a list containing the filenames of all selected files and subfolders in the current folder. |
This method is available in PyGTK 2.4 and above.
The get_filenames() method returns a list containing all the selected files and subfolders in the current folder of the chooser. The returned names are full absolute paths. If files in the current folder cannot be represented as local filenames they will be ignored. (See the get_uris() method for more information)
|
filename : | the full path of the new current folder |
Returns : | TRUE if the folder could be changed successfully, FALSE otherwise. |
This method is available in PyGTK 2.4 and above.
The set_current_folder() method sets the current folder for the chooser to the local filename specified by filename. The user will be shown the full contents of the current folder, plus user interface elements for navigating to other folders.
|
Returns : | the full path of the current folder, or None if the current path cannot be represented as a local filename. |
This method is available in PyGTK 2.4 and above.
The get_current_folder() method returns the current folder of the chooser as a local filename. See the set_current_folder() method for more information.
|
Returns : | The currently selected URI, or None if no file is selected. |
This method is available in PyGTK 2.4 and above.
The get_uri() method returns the URI for the currently selected file in the file selector. If multiple files are selected, one of the filenames will be returned at random. If the file chooser is in folder mode, this function returns the selected folder.
|
uri : | the URI to set as the current file |
Returns : | TRUE if both the folder could be changed and the URI was successfully selected. |
This method is available in PyGTK 2.4 and above.
The set_uri() method sets the file referred to by uri as the current file for the file chooser; If the file name isn't in the current folder of the chooser, then the current folder of the chooser will be changed to the folder containing uri. This is equivalent to the sequence of unselect_all() followed by select_uri(). Note that the file must exist, or nothing will be done except for the directory change. To pre-enter a filename for the user, as in a "Save As ..." dialog, use the set_current_name() method.
|
uri : | the URI of the file to select |
Returns : | TRUE if both the folder could be changed and the URI was successfully selected. |
This method is available in PyGTK 2.4 and above.
The select_uri() method selects the file referred to by uri. If the URI doesn't refer to a file in the current folder of the chooser, then the current folder of the chooser will be changed to the folder containing the file referenced by uri.
|
uri : | the URI of the file to unselect |
This method is available in PyGTK 2.4 and above.
The unselect_uri() method unselects the file referred to by uri. If the file is not in the current directory, does not exist, or is otherwise not currently selected, this method does nothing.
|
Returns : | a list containing the URIs of all selected files and subfolders in the current folder. |
This method is available in PyGTK 2.4 and above.
The get_uris() method returns a list containing all the selected files and subfolders in the current folder of the chooser. The returned names are full absolute URIs.
|
uri : | the URI for the new current folder |
Returns : | TRUE if the folder could be changed successfully, FALSE otherwise. |
This method is available in PyGTK 2.4 and above.
The set_current_folder_uri() method sets the current folder for the chooser to the folder referenced by uri. The user will be shown the full contents of the current folder, plus user interface elements for navigating to other folders.
|
Returns : | the URI for the current folder. |
This method is available in PyGTK 2.4 and above.
The get_current_folder_uri() method returns the URI reference of the current folder of the chooser. See the set_current_folder_uri() method for more information.
|
preview_widget : | a widget for displaying a preview. |
This method is available in PyGTK 2.4 and above.
The set_preview_widget() method sets the "preview-widget" property to the value of preview_widget. The preview_widget is used to preview the currently selected file. To implement a custom preview:
When there is no application-supplied preview widget, or the application-supplied preview widget is not active, the file chooser may display an internally generated preview of the current file or it may display no preview at all.
|
Returns : | the current preview widget, or None |
This method is available in PyGTK 2.4 and above.
The get_preview_widget() method returns the value of the "preview_widget" property i.e. the current preview widget. See the set_preview_widget() method for more information.
|
active : | if TRUE, display the user-specified preview widget |
This method is available in PyGTK 2.4 and above.
The set_preview_widget_active() method sets the "preview_widget_active" property to the value of active. If active is TRUE, the preview widget set by the set_preview_widget() method should be shown for the current filename. When active is FALSE, the file chooser may display an internally generated preview of the current file or it may display no preview at all. See the set_preview_widget() for more details.
|
Returns : | TRUE if the preview widget is active for the current filename. |
This method is available in PyGTK 2.4 and above.
The get_preview_widget_active() method returns the value of the "preview-widget-active" property that indicates whether the preview widget set by the set_preview_widget() method should be shown for the current filename. See the set_preview_widget_active() method for more details.
|
use_label : | if TRUE, display a stock label with the name of the previewed file |
This method is available in PyGTK 2.4 and above.
The set_use_preview_label() method sets the "use-preview-label" property to the value of use_label. If use_label is TRUE (the default), the file chooser should display a stock label with the name of the file that is being previewed. Applications that want to draw the whole preview area themselves should set this to FALSE and display the name themselves in their preview widget. See the set_preview_widget() method for more information.
|
Returns : | TRUE if the file chooser is set to display a label with the name of the previewed file; FALSE otherwise. |
This method is available in PyGTK 2.4 and above.
The get_use_preview_label() method returns the value of the "use-preview-label" property that indicates whether a stock label should be drawn with the name of the previewed file. See the set_use_preview_label() for more information.
|
Returns : | the filename to preview, or None. |
This method is available in PyGTK 2.4 and above.
The get_preview_filename() method returns the filename that should be previewed in a custom preview widget or None if no file is selected, or if the selected file cannot be represented as a local filename. See the set_preview_widget() method for more details.
|
Returns : | the URI for the file to preview, or None. |
This method is available in PyGTK 2.4 and above.
The get_preview_uri() method returns the URI of the file that should be previewed in a custom preview widget or None if no file is selected. See the set_preview_widget() method fr more details.
|
extra_widget : | the widget to display extra options |
This method is available in PyGTK 2.4 and above.
The set_extra_widget() method sets the "extra-widget" property to the value of extra_widget. extra_widget is an application-supplied widget used to display extra options to the user.
|
Returns : | the current extra widget, or None. |
This method is available in PyGTK 2.4 and above.
The get_extra_widget() method returns the value of the "extra-widget" property that contains either a widget used to display extra options to the user or None if no extra widget is in use. See the set_extra_widget() for more information.
|
filter : | a gtk.FileFilter |
This method is available in PyGTK 2.4 and above.
The add_filter() method adds the gtk.FileFilter specified by filter to the list of filters that the user can select from. When a filter is selected, only files that are passed by that filter are displayed.
|
filter : | a gtk.FileFilter |
This method is available in PyGTK 2.4 and above.
The remove_filter() method removes the gtk.FileFilter specified by filter from the list of filters that the user can select from.
|
Returns : | a list containing the current set of user selectable filters. |
This method is available in PyGTK 2.4 and above.
The list_filters() method returns the current set of user-selectable filters. See the add_filter() and remove_filter() method for more details.
|
filter : | a gtk.FileFilter |
This method is available in PyGTK 2.4 and above.
The set_filter() method sets the "filter" property to the value of filter and also sets the current filter to filter. Only the files that pass filter will be displayed. If the user-selectable list of filters is non-empty, then filter should be one of the filters in that list. Setting the current filter when the list of filters is empty is useful if you want to restrict the displayed set of files without letting the user change it.
|
Returns : | the current filter, or None. |
This method is available in PyGTK 2.4 and above.
The get_filter() method returns the value of the "filter" property which is the current filter. See the set_filter() method for more information.
|
folder : | the filename of the folder to add |
Returns : | TRUE if the folder could be added successfully. |
This method is available in PyGTK 2.4 and above.
The add_shortcut_folder() adds the folder specified by folder the list of shortcut folders in a file chooser. Shortcut folders are displayed at the upper left in the gtk.FileChooser. By double-clicking on a shortcut the user can open that folder directly. Note that shortcut folders do not get saved, as they are provided by the application. For example, you can use this to add a "/usr/share/mydrawprogram/Clipart" folder to the volume list.
The GError exception is raised if an error occurred while adding the folder.
|
folder : | the filename of the folder to remove |
Returns : | TRUE if folder was removed from the list of shortcut folders. |
This method is available in PyGTK 2.4 and above.
The remove_shortcut_folder() method removes the folder specified by folder from a file chooser's list of shortcut folders. remove_shortcut_folder() returns TRUE if successful. See the add_shortcut_folder() method for more information.
The GError exception is raised if an error occurred while removing the folder.
|
Returns : | A list of shortcut folder filenames, or None if there are no shortcut folders. |
This method is available in PyGTK 2.4 and above.
The list_shortcut_folders() method returns the list of shortcut folders in the file chooser, as set by the add_shortcut_folder() method or None if there are no shortcut folders. It is not possible to get a list of the user-specified shortcut folders.
|
uri : | the URI of the folder to add |
Returns : | TRUE if the folder was added |
This method is available in PyGTK 2.4 and above.
The add_shortcut_folder_uri() method adds a folder with the URI specified by uri to the list of shortcut folders in a file chooser. Note that shortcut folders do not get saved, as they are provided by the application. For example, you can use this to add a "file:///usr/share/mydrawprogram/Clipart" folder to the volume list. See the add_shortcut_folder() method for more details.
The GError exception is raised if an error occurred while adding the folder.
|
uri : | URI of the folder to remove |
Returns : | TRUE if the folder was removed. |
This method is available in PyGTK 2.4 and above.
The remove_shortcut_folder_uri() method removes the folder with the URI specified by uri from the file chooser's list of shortcut folders.
The GError exception is raised if an error occurred while removing the folder.
|
Returns : | a list of shortcut folder URIs, or None |
This method is available in PyGTK 2.4 and above.
The list_shortcut_folder_uris() method returns a list of the shortcut folders in the file chooser, as set by the add_shortcut_folder_uri() method. It is not possible to get a list of the user-specified folder URIs.
|
show_hidden : | if TRUE hidden files and folders should be displayed. |
This method is available in PyGTK 2.6 and above.
The set_show_hidden() method sets the "show-hidden" property to the value of show_hidden. If show_hidden is TRUE, hidden files and folders should be displayed in the file selector.
|
Returns : | TRUE if hidden files and folders are displayed. |
This method is available in PyGTK 2.6 and above.
The get_show_hidden() method returns the value of the "show-hidden" property that indicates whether hidden files and folders should be displayed in the file selector. See the set_show_hidden() method for more information.
|
do_overwrite_confirmation : | if TRUE ask the user for confirmation before overwriting a file in save mode. |
This method is available in PyGTK 2.8 and above.
The set_do_overwrite_confirmation() method sets the "do-overwrite-confirmation" property to the value of do_overwrite_confirmation. If do_overwrite_confirmation is TRUE and the "action" property is set to gtk.FILE_CHOOSER_ACTION_SAVE, the file chooser will present a confirmation dialog if the user attempts to overwrite an existing file. The default value is FALSE.
If all you need is the stock confirmation dialog, set the "do-overwrite-confirmation" property to TRUE. You can override the way confirmation is done by actually handling the "confirm-overwrite" signal; please refer to its documentation for details.
|
Returns : | TRUE if hidden files and folders are displayed. |
This method is available in PyGTK 2.8 and above.
The get_do_overwrite_confirmation() method returns the value of the "do-overwrite-confirmation" property that indicates whether a user is asked for confirmation before overwriting an existing file. See the set_do_overwrite_confirmation() method for more information.
|
filechooser : | the filechooser widget that received the signal |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
Returns : | one of the GTK FileChooser Confirmation Constants indicating what action GTK+ should take. |
This signal is available in PyGTK 2.8 and above.
The "confirm-overwrite" signal is emitted when it is appropriate to present a confirmation dialog when the user has selected a file name that already exists. The signal is only emitted when the file chooser "action" property is set to gtk.FILE_CHOOSER_ACTION_SAVE.
Most applications just need to turn on the "do-overwrite-confirmation" property (or call the set_do_overwrite_confirmation() method), to automatically get a stock confirmation dialog. Applications that need to customize this behavior should do that, and also connect to the "confirm-overwrite" signal.
A signal handler for this signal must return one of the GTK FileChooser Confirmation Constants that indicates the action to take. If the handler determines that the user wants to select a different filename, it should return gtk.FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN. If it determines that the user is satisfied with his choice of file name, it should return gtk.FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME. On the other hand, if it determines that the stock confirmation dialog should be used, it should return gtkfile_CHOOSER_CONFIRMATION_CONFIRM. The following psuedo-code example illustrates this.
def confirm_overwrite_callback(chooser): uri = chooser.get_uri() if is_uri_read_only(uri): if user_wants_to_replace_read_only_file (uri): return gtk.FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME else return gtk.FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN else # fall back to the default dialog return gtk.FILE_CHOOSER_CONFIRMATION_CONFIRM ... chooser = gtk.FileChooserDialog(...) chooser.set_do_overwrite_confirmation(TRUE) chooser.connect("confirm-overwrite", confirm_overwrite_callback) if chooser.run() == gtk.RESPONSE_ACCEPT: save_to_file(chooser.get_filename() chooser.destroy() |
|
filechooser : | the filechooser widget that received the signal |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in PyGTK 2.4 and above.
The "current-folder-changed" signal is emitted when the current folder displayed in filechooser is changed. Normally you do not need to connect to this signal, unless you need to keep track of which folder a file chooser is showing.
|
filechooser : | the filechooser widget that received the signal |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in PyGTK 2.4 and above.
The "file-activated" signal is emitted when the user double-clicks on a file (not a folder) or presses Enter. Normally you do not need to connect to this signal. It is used internally by gtk.FileChooserDialog to know when to activate the default button in the dialog.
|
filechooser : | the filechooser widget that received the signal |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in PyGTK 2.4 and above.
The "selection-changed" signal is emitted when the file selection in filechooser is changed either by clicking on a filename or by changing the current folder. Normally you do not need to connect to this signal, as it is easier to wait for the file chooser to finish running, and then to get the list of selected files using the functions mentioned below.
|
filechooser : | the filechooser widget that received the signal |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in PyGTK 2.4 and above.
The "update-preview" signal is emitted when the preview when a file chooser should be regenerated. For example, this can happen when the currently selected file changes. You should use this signal if you want your file chooser to have a preview widget.
Once you have installed a preview widget with the set_preview_widget() method, you should update it when this signal is emitted. You can use the methods get_preview_filename() or get_preview_uri() to get the name of the file to preview. Your widget may not be able to preview all kinds of files so your callback must call the set_preview_widget_active() to inform the file chooser if the preview was generated successfully or not.
Please see the example code in the section called Adding a Preview Widget.
Also see the set_preview_widget(), set_preview_widget_active(), set_use_preview_label(), get_preview_filename() and get_preview_uri() methods for more information