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)