Tkinter File Dialog Question

Jørgen Cederberg jorgencederberg at hotmail.com
Mon Nov 4 08:54:30 EST 2002


"Steve Zatz" <slzatz at hotmail.com> wrote in message news:6747bc9d.0211040501.2e8000b5 at posting.google.com...
> 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.

Hi Steve,

I don't think that the dialog returns anything but the complete filename. So assuming that you get the complete filename, i.e. 'myfilename.xml', you could just split the name at the '.' to get the extension. 

>>> filename = 'myfilename.xml'
>>> extension = filename.split('.')[-1]
>>> extension
'xml'

Regards
Jorgen





> 
> 
> 
> > 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



More information about the Python-list mailing list