Restricting the file extension saved when using tkFileDialog.asksaveasfile
python, tkinter, user-interface
Solution
You can specify file type by using the 'filetypes' option. The below example will change the file type drop down on the save dialog to .espace and all files.
filename = tkFileDialog.asksaveasfilename(defaultextension=".espace", filetypes=(("espace file", "*.espace"),("All Files", "*.*") ))
I have not found an option, using asksaveasfilename, to restrict use of other file extensions. I'd think you could write a loop that would force the user to use that extension:
import os
import sys
ext = ""
while ext != ".escape":
filename = tkFileDialog.asksaveasfilename(defaultextension=".espace", filetypes=(("espace file", "*.espace"),("All Files", "*.*") ))
file,ext = os.path.splitext(filename)
Problem
I am currently writing a GUI in python using Tkinter where the user is able to save the state of the GUI in their directory with the extension ".espace". `filename = tkFileDialog.asksaveasfilename(defaultextension=".espace")` If the user specifies a different file extension then the file is saved with the extension that they specified. Is there anyway to prevent this? So that they are completely restricted to saving the file with the extension ".espace"?