tkinter, askopenfilename, initial directory

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Aug 12 01:48:28 EDT 2008


En Thu, 07 Aug 2008 19:08:56 -0300, Lisa Frauens <frauens at hotmail.com>  
escribi�:

> I see there is an initial directory option in askopenfilename.  However,  
> I want the initial directory to be the last directory used in a prior  
> execution of my script.  Is there a way to force the askopenfilename to  
> the previous initial directory easily using information somewhere (I  
> know Windows applications natively do this so you don't have to navigate  
> all the way back to directory of interest)?  It defaults to the current  
> working directory. or do I have to save this initial directory  
> somewhere, probably in a *.txt file,  when running my script the first  
> time and then access this *.txt file, and use information as the initial  
> directory option in askopenfilename, when running the script a  
> subsequent time?

Yes, you should save the last used directory somewhere. Try using  
ConfigParser.

last_used_directory = ... # retrieve from saved options; default = "."
filename = tkFileDialog.askopenfilename(initialdir=last_used_directory)
if filename is not None:
     # save the directory: os.path.dirname(filename)
     ...

-- 
Gabriel Genellina




More information about the Python-list mailing list