I'm new to UI developement with Gtk and ran into something I didn't expect. The FileChooser automatically sorts by name, regardless to if it's a file or directory. I like having directories listed first, and people are used to/expect it.
Is there some way I can get FileChooser to behave this way?
EDIT: In most of the major visual file managers, it is the default behavior to list the directories before the files. These links show what people typically see in their file managers: konqueror, nautilus, thunar, windows, osx and this is what I'm seeing with my Gtk FileChooser. Is there a way I can get it to look like the rest of the file managers by default, using code?
EDIT2, the code I open it with:
dialog=Gtk.FileChooserDialog("Select a file",self,
Gtk.FileChooserAction.OPEN,(
Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
response=dialog.run()
Thanks to Fabby and ThorSummoner's comments, I've stumbled across the closest solution. Using GSettings, I can change the FileChooser settings from the app, though only globally. This should be okay in this case, since the user will likely prefer to have the same experience with all Gtk based FileChoosers on their system.
from gi.repository import Gio
setting = Gio.Settings.new("org.gtk.Settings.FileChooser")
setting.set_boolean("sort-directories-first", True)
As expected, setting it to False will only sort by the names and not group the directories together.
The setting can also be bound to a control with Gio.Settings.bind()
I've opted for a setting switch that the user will be able to set for their preference.
In my testing [snippet below] the gtk file chooser always lists folders before files.
Example Adapted from: https://developer.gnome.org/gtk3/stable/GtkFileChooserDialog.html Python docs at: http://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Dialog.html#Gtk.Dialog.run
#!/usr/bin/env python3
from gi.repository import Gtk
from pprint import pprint
dialog = Gtk.FileChooserDialog(
title="Open File"
)
res = dialog.run()
pprint(res)
dialog.destroy()
Note: If you run this, you can exit the GTK gui with clt+f4, it does not exit on normal signals due to example simplicity. You may also end the python process you started from a task manager.
Versions:
$ python3 --version
Python 3.4.0
$ python3
>>> from gi.repository import Gtk
>>> Gtk._version
'3.0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With