so I've reverse-engineered Tkinter's tkfiledialog functions askopenfilename and asksaveasfilename to figure out how they work:

import Tkinter

class Open:
    "Ask for a filename to open"
    def __init__(self, master=None, **options):
        self.master, self.options = master, options
        if not master and options.get('parent'): self.master = options['parent']

    def _fixoptions(self):
        try: self.options["filetypes"] = tuple(self.options["filetypes"]) # make sure "filetypes" is a tuple
        except KeyError: pass

    def _fixresult(self, widget, result):
        if isinstance(result, tuple):
            # multiple results:
            result = tuple([getattr(r, "string", r) for r in result])
            if result:
                import os
                path, file = os.path.split(result[0])
                self.options["initialdir"] = path
                # don't set initialfile or filename, as we have multiple of these
            return result
        if not widget.tk.wantobjects() and "multiple" in self.options:
            # Need to split result explicitly
            return self._fixresult(widget, widget.tk.splitlist(result))

        if result:
            # keep directory and filename until next time
            import os
            # convert Tcl path objects to strings
            try: result = result.string
            except AttributeError: pass # it already is a string
            path, file = os.path.split(result)
            self.options["initialdir"] = path
            self.options["initialfile"] = file
        self.filename = result # compatibility
        return result

    def show(self):
        self._fixoptions()
        # we need a dummy widget to properly process the options
        # (at least as long as we use Tkinter 1.63)
        w = Tkinter.Frame(self.master)
        try:
            s = w.tk.call("tk_getOpenFile", *w._options(self.options))
            s = self._fixresult(w, s)
        finally:
            try: w.destroy() # get rid of the widget
            except: pass
        return s


class SaveAs:
    "Ask for a filename to save as"
    def __init__(self, master=None, **options):
        self.master, self.options = master, options
        if not master and options.get('parent'): self.master = options['parent']

    def _fixoptions(self):
        try: self.options["filetypes"] = tuple(self.options["filetypes"]) # make sure "filetypes" is a tuple
        except KeyError: pass

    def _fixresult(self, widget, result):
        if result:
            # keep directory and filename until next time
            import os
            # convert Tcl path objects to strings
            try: result = result.string
            except AttributeError: pass # it already is a string
            path, file = os.path.split(result)
            self.options["initialdir"] = path
            self.options["initialfile"] = file
        self.filename = result # compatibility
        return result

    def show(self):
        self._fixoptions()
        # we need a dummy widget to properly process the options
        # (at least as long as we use Tkinter 1.63)
        w = Tkinter.Frame(self.master)
        try:
            s = w.tk.call("tk_getSaveFile", *w._options(self.options))
            s = self._fixresult(w, s)
        finally:
            try: w.destroy() # get rid of the widget
            except: pass
        return s

to use these, simply call:

Open( options ).show()
SaveAs( options ).show()

the problem with this is knowing which filter was selected upon opening/saving the file.
you can't access w.tk so getting the filter from the interface is near impossible.

does anyone know of a lightweight alternative that can do this??
(I don't need the extra bulk of something like wx or Qt just for the file dialogs)

ok... forget what I said about near impossible, I just looked in tk.tcl and found this:

if {![llength [info commands tk_getOpenFile]]} {
    proc ::tk_getOpenFile {args} {
    if {$::tk_strictMotif} {
        return [tk::MotifFDialog open {*}$args]
    } else {
        return [::tk::dialog::file:: open {*}$args]
    }
    }
}
if {![llength [info commands tk_getSaveFile]]} {
    proc ::tk_getSaveFile {args} {
    if {$::tk_strictMotif} {
        return [tk::MotifFDialog save {*}$args]
    } else {
        return [::tk::dialog::file:: save {*}$args]
    }
    }
}

idk anything about the tk backend, but if I can figure it out, I can most likely rebuild the dialogs locally to have that return the filter info.

or heck if the selected filter is stored in a var, I may be able to access that.

EDIT:
nope, looks like I've hit a dead-end...
beyond that last bit is the binaries...

unless I can get the src for those dialogs from Tk, I'mma have to look for a replacement...

you know what... screw it... my UI uses OpenGL, so why not do what Blender does and make it handle files too... heh

instead of being a hippocrite and using Tkinter when I've been saying I've wanted to build a file importer.

This article has been dead for over six months. Start a new discussion instead.