1

I've seen this question that explains how to restrict a gtk FileChooserDialog so that only directories are allowed.

I would like to place a further restriction so that only directories whose name match a certain pattern are allowed. I tried using a gtk.FileFilter() but it did not work:

self.logFileBrowseDialog = gtk.FileChooserDialog(title="Select Log Directory",
                       action=gtk.FILE_CHOOSER_ACTION_SAVE,
                       buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,
                                gtk.STOCK_OPEN,gtk.RESPONSE_OK))

self.logFileBrowseDialog.set_current_folder('/path/to/default/log/dir');
logDirFilter = gtk.FileFilter()
logDirFilter.set_name("Log Directory")
logDirFilter.add_pattern("[0-1][0-9]-[0-3][0-9]-[0-9][0-9][0-9][0-9]-[0-2][0-9]-[0-5][0-9]-[0-5][0-9].[0-9][0-9][0-9]_GMT_Logs")
self.logFileBrowseDialog.add_filter(logDirFilter)
self.logFileBrowseDialog.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)

Is it possible to only permit directories of a certain name? I realize that the user still needs a way to navigate away from an non-permitted directory to a permitted one. Ideally the "Open" button would be disabled if the current location does not meet the name criteria.

Community
  • 1
  • 1
tony_tiger
  • 789
  • 1
  • 11
  • 25
  • In what way did it not work? Did it exclude too many, not exclude enough, throw an error, ... – zondo Apr 05 '16 at 01:39
  • I think the only way to make such restrinction is either: 1.- Make your own file choser dialog 2.- Hooked the native gtk dialog to edit the browser dialog. – Joel Apr 05 '16 at 01:47
  • @zondo It simply allowed all directories as if there was no filter. (Sorry, I should have mentioned this.) – tony_tiger Apr 05 '16 at 07:13
  • 1
    For now, it appears that there's no easy way to do this. `Gtk.FileFilter` only applies to files. And it doesn't look like there's a `Gtk.DirFilter` or something like that. – Sylvester Kruin Nov 06 '21 at 18:41

0 Answers0