LaLa
LaLa

Reputation: 121

python save file dialogue using tkinter library

I created my first python GUI using tkinter library, all goes smoothly except when I want to save the file. The app basically opens a word document, edit some text then save the edited file. It saved perfectly with the code below:

file_name = "{}".format(save_entry.get())
save_filename = file_name + '.docx'
word_document.save(save_filename)

But I wanted to use askopenfilename, as it enables me to specify the file location.

Here is my attempt, using the argument initialfile, it saves an empty doc file, not the edited one.

filedialog.asksaveasfile(initialfile=word_document.save(save_filename), filetypes=[("Word Document Files", "*.docx"), ("All Files", "*.*")])

Upvotes: 1

Views: 807

Answers (1)

LaLa
LaLa

Reputation: 121

The problem I faced was using askopenfilename creates an empty docx file, and not reflecting the update made on that file. So, referring to the Tkinter Dialogs documentation there is another dialog to select a directory only.

I used it with the code above and now it works as needed.

file_name = "{}".format(save_entry.get())
save_filename = file_name + '.docx'
directory = filedialog.askdirectory()
word_document.save(directory+'/'+save_filename)

Upvotes: 1

Related Questions