Tkinter File Dialog Question

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Mon Nov 4 08:34:25 EST 2002


On Mon, 2002-11-04 at 13:15, Martin Franklin wrote:
> On Mon, 2002-11-04 at 13:01, Steve Zatz wrote:
> > Jorgen,
> > 
> > Thanks much for the response.  I wasn't aware that there was a
> > defaultextension option.  Unfortunately, it doesn't quite solve my
> > problem.  I am providing the users with several extension options (eg
> > xml, txt) and they are selecting one of those options and then
> > (understandably) creating a file name without an extension assuming
> > that it will pick up the extension they have selected.  However, I
> > don't see any way to get at their extension selection since the Dialog
> > only seems to return the filename they input.
> > 
> > 
> > 
> > > you can use the option: 'defaultextension'. Example
> > > 
> > > >>> import tkFileDialog
> > > >>> res = tkFileDialog.asksaveasfilename(defaultextension=".txt")
> > >  (user types in "myfilename")
> > > >>> res
> > > 'myfilename.txt'
> > > 
> > > Remember the dot in the extension string. I just tried and it works, 
> > > unless the user selects a filename that already exist.
> > > 
> > > Regards
> > > Jorgen Cederberg
> > -- 
> 
> Steve,
> 
> Have you tried this.  On my linux (RedHat 8.0) python 2.2.2
> I can supply a list of filetypes to the tkFileDialog and when the user
> only types a file name (no extension) the selected extension is appended
> to the returned filename.  :-
> 
> 
> 
> 
> >>> import tkFileDialog
> >>> filename = tkFileDialog.asksaveasfilename(filetypes=[("text files",
> "*.txt"),
> ...     ("python source code", "*.py")])
> 
> <<< users types aaaa and selected *.txt as file type>>>>
> 
> 
> >>> print filename
> /usr/python/scripts/aaaa.txt
> 


Whoops..  I have compiled python with Tk 8.4 this seems to be a 'new'
feature.... I just tested against Tk 8.3 and my solution doesn't work!

Sorry but I guess short of upgrading your Tk version..... 










More information about the Python-list mailing list