954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

asksaveasfilename()... setting the extension... please help

so I'm using the 'initialfile' to automatically generate the name of the import file,
but I'm using 'defaultextension' to set the extension when clicking 'Save'

how do I set the 'defaultextension' based on the current filter selection??

Tcll
Posting Whiz in Training
238 posts since Jan 2010
Reputation Points: 6
Solved Threads: 0
 
# the Tkinter filedialog for saving file
# tkfd.asksaveasfile(**options) returns file handle to save to

try:
    # Python2
    import Tkinter as tk
    import tkFileDialog as tkfd
except ImportError:
    # Python3
    import tkinter as tk
    import tkinter.filedialog as tkfd

mask = [
    ("Text files","*.txt"),
    ("Python files","*.py *.pyw"),
    ("All files","*.*")]

# if the filename does not have extension
# it will add the specified defaultextension
file_save = "test77"
fout = tkfd.asksaveasfile(
    title="testing defaultextension='.txt'",
    initialdir="C:/Python27/Atest27/Bull",
    initialfile=file_save,
    defaultextension=".txt",
    filetypes=mask)

# test write a file
data = """\
1
2
3
4
"""
fout.write(data)
fout.close()
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

well WTF D:<

I'm using this:

exfl = asksaveasfilename(
    initialfile=str((str(imfl).split("/")[-1]).split('.')[0]),
    filetypes=ex_types,
    defaultextension='.obj') #small bug (you have to type the extension before saving)


ex_types is a generated list returned from the plugins...
but it's returnng the types as it should...

and yours works
but yet mine doesn't D:

Tcll
Posting Whiz in Training
238 posts since Jan 2010
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You