FileFilter

A FileFilter object is used in conjunction with FileChooser-based widgets, to limit the type of files which can be viewed. For example, to limit a FileChooser to only view music files, a FileFilter can be used.

Constructor

The FileFilter can be constructed using the following:

filefilter = Gtk.FileFilter()

Note

A FileFilter must be created for each file type which is to be filtered.

Methods

To set the name of the FileFilter use:

filefilter.set_name(name)

Retrieving the name of a particular filter is possible using:

filefilter.get_name()

To limit files which can be displayed by extension use:

filefilter.add_pattern(pattern)

The pattern parameter should be a string, which uses wildcard entries. For example using "*.flac" would permit only files which end with the .flac extension.

Files can also be limited according to their mime-type:

filefilter.add_mime_type(mime_type)

A mime_type is a content filter which uses content within the header of the file to identify it. Example of a mime types are "image/png", "video/mp4", and "text/html".

Example

Below is an example of a FileFilter:

#!/usr/bin/env python3

from gi.repository import Gtk

class FileFilter(Gtk.FileChooserDialog):
    def __init__(self):
        Gtk.FileChooserDialog.__init__(self)
        self.add_button("_Cancel", Gtk.ResponseType.CLOSE)
        self.add_button("_Open", Gtk.ResponseType.OK)
        self.connect("response", self.on_response)

        filefilter = Gtk.FileFilter()
        filefilter.set_name("All Items")
        filefilter.add_pattern("*")
        self.add_filter(filefilter)

        filefilter = Gtk.FileFilter()
        filefilter.set_name("Audio")
        filefilter.add_mime_type("audio/flac")
        filefilter.add_mime_type("audio/ogg")
        self.add_filter(filefilter)

        filefilter = Gtk.FileFilter()
        filefilter.set_name("Images")
        filefilter.add_pattern("*.png")
        filefilter.add_pattern("*.jpg")
        filefilter.add_pattern("*.bmp")
        self.add_filter(filefilter)

    def on_response(self, filechooserdialog, response):
        filechooserdialog.destroy()

dialog = FileFilter()
dialog.run()

Download: FileFilter