Python: Save a file based on user input
Question: I am attempting to save a file from a python tkinter window via a 'Save As'
prompt. I have looked for a while now and cannot seem to find the answer I am
looking for. I can successfully save the information to a file with a default
name, and even can save it using a name the user inputs via input(), however,
this is not what I am trying to do. I want the user to be able to click, 'Save
As' and then when the prompt comes up, they enter in the file name and it
saves as that name, I just cannot seem to find an answer anywhere. Here is my
code at this point:

    
    
    # Import Tkinter
    from tkinter import *
    import os
    import pickle
    from tkinter.filedialog import askopenfilename, asksaveasfile
    from tkinter.messagebox import *
    
    MainWindow = Tk()
    MainWindow.geometry("600x400")
    MainWindow.attributes("-alpha", 1)
    MainWindow.title(string="Hours Log")
    CurrentDirect=os.getcwd()
    
    def FileSaveAs():
    fname = asksaveasfile(initialdir=CurrentDirect ,filetypes=(("Text Files", "*.txt"),
                                           ("All files", "*.*") ))
    if fname:
            try:
                print(fname)
                SH = SHVar.get()
                SM = SMVar.get()
                SAP = SAPVar.get()
                EH = EHVar.get()
                EM = EMVar.get()
                EAP = EAPVar.get()
                DM = DMVar.get()
                DD = DDVar.get()
                DY = DYVar.get()
                DE = Description.get("1.0", END)
                AP = APVar.get()
                with open("filename.txt", 'wb') as f:
                    pickle.dump([SH, SM, SAP, EH, EM, EAP, DM, DD, DY, DE, AP], f)
    
            except:
                showerror("FILE SAVE ERROR", "Error on Saving File!\n'%s'" % fname)
            return
    

I understand that the "filename.txt" is the name of the file to save to,
however, how do I acquire the variable name from the prompt?

NOTE: There are no errors in this code, it runs fine with the rest of my
program.


Answer: You can use `asksaveasfilename` instead of `asksaveasfile` and `fname` instead
of `"filename.txt"`.

    
    
      60   def asksaveasfile(self):
      61 
      62     """Returns an opened file in write mode."""
      63 
      64     return tkFileDialog.asksaveasfile(mode='w', **self.file_opt)
      65 
      66   def asksaveasfilename(self):
      67 
      68     """Returns an opened file in write mode.
      69     This time the dialog just returns a filename and the file is opened by your own code.
      70     """
    

