0

I'm using PyGTK's GtkFileChooserButton, which is working - but looks very weird on Windows environment. Is it possible to use native Windows file chooser dialog?

UPDATE

See the comments for possible directions. However, if you decide (like me...) that it doesn't worth the effort, this will make the Gtk FileChooser more tolerable:

def get_win_my_documents():
    # based on http://stackoverflow.com/questions/3858851/python-get-windows-special-folders-for-currently-logged-in-user
    # and http://stackoverflow.com/questions/6227590/finding-the-users-my-documents-path

    CSIDL_PERSONAL = 5       # My Documents
    # the 2 stackoverflow answers use different values for this constant!
    SHGFP_TYPE_CURRENT = 0   # Get current, not default value

    buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
    ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)

    if os.path.isdir(buf.value):
        return buf.value
    else:
        # fall back to simple "home" notion
        return(os.path.expanduser("~"))

...

    my_documents = get_win_my_documents()
    chooser.set_current_folder(my_documents)
    chooser.add_shortcut_folder(my_documents)

    # I'm not sure if it's a general solution, but works for me...
    downloads = os.path.join(os.path.expanduser("~"), "Downloads")
    if os.path.isdir(downloads):
        chooser.add_shortcut_folder(downloads)
Zvika
  • 1,542
  • 2
  • 17
  • 21
  • 2
    The next version of GTK+ (3.20) [appears to introduce this functionality](https://developer.gnome.org/gtk3/unstable/gtk3-GtkFileChooserNative.html). Until then, or if you must target an earlier version of GTK+, you will have to do it yourself. Sorry. – andlabs Dec 07 '15 at 16:41
  • Thanks. Somehow, your comment helped me find this: http://faq.pygtk.org/index.py?file=faq21.013.htp&req=show&sa=U&ved=0ahUKEwjPluGXosrJAhWBHg8KHQqZCEEQFggTMAQ&usg=AFQjCNF5cbLU2_tkE3YG4Py3wf0QBx4t4g - not a complete answer, but indeed an interesting direction – Zvika Dec 07 '15 at 18:18
  • Right. `GetOpenFileName()` is a deprecated API and might feel a bit clunky, but if you still need XP support you're stuck with it in terms of native APIs. Be sure to read the [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646927%28v=vs.85%29.aspx) [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646839%28v=vs.85%29.aspx). For Vista and newer you can use the Common Item Dialog (IFileDialog), but I have no clue how to use it from Python. – andlabs Dec 07 '15 at 18:28
  • I also don't know if you should do this, but to get the same behavior that GtkDialog does with making the dialog modal you'll need to get the HWND that corresponds to your GtkWindow (via its GdkWindow) and set that as the owner window of the dialog. Again, no guarantees that this will have undesirable effects on GTK+ itself, as I personally haven't tried this, but if you absolutely need this open file dialog to block the main window... – andlabs Dec 07 '15 at 18:29
  • Do you really still use old PyGTK? – Bachsau Apr 24 '18 at 04:52

0 Answers0