Tkinter is a popular Python library for building graphical user interfaces (GUIs). It provides a variety of widgets and functions that can be used to create and display dialog boxes, such as message boxes, file dialogs, and color dialogs. In this tutorial, we will learn how to create a save file dialog in Tkinter, which allows the user to select a location anywhere on their device to save a file.
Save File Dialogs in Tkinter
Let’s create our first save file dialog with Tkinter!
First we need to make additional import from the Tkinter module (if you haven’t done import * from tkinter
). You can think of “filedialog” as a submodule inside Tkinter which allows us to create various types of dialogs.
import tkinter as tk
from tkinter import filedialog
Now all we have to do is use the asksaveasfile()
method, and pass in the required parameters.
There are no “compulsory” parameters (we can just leave everything to the default settings). But we went ahead anyway of defined title
and default extension
parameters anyway. Here is the full code.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
def save_file():
file = filedialog.asksaveasfile(title = "Save File",
defaultextension=".txt")
if file:
file.write("This is the content of the file.")
file.close()
save_button = tk.Button(root, text="Save File", command=save_file)
save_button.pack()
root.mainloop()
The default extension is the automatic extension applied to the save file if you did not pick one. The title parameter controls the text that appears on the top-left corner of the dialog.
Let’s take a look at some other possible customizations.
One thing we can do is control where the Save File Dialog shows up (initially). By passing in a directory path in the initialdir
parameter, this becomes possible.
The below code demonstrates this (Save Dialog will show up in C-drive).
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
def save_file():
file = filedialog.asksaveasfile(title = "Save File",
initialdir = "C:/",
defaultextension=".txt")
if file:
file.write("This is the content of the file.")
file.close()
save_button = tk.Button(root, text="Save File", command=save_file)
save_button.pack()
root.mainloop()
Another important feature is the “filetypes” parameter. We can define various “file types” (e.g text or Excel), which the user can select from when saving his file (the filetype selected will become the extension of the saved file)
Pay attention to how the filetypes are defined. We have an array of tuples. Each tuple has two values, the first being the label, and the second being the actual extension.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
def save_file():
types = [("Text files", "*.txt"),
("Excel files", "*.xlsx"),
("All files", "*.*")]
file = filedialog.asksaveasfile(title = "Save File",
initialdir = ".",
defaultextension=".txt",
filetypes=types)
if file:
file.write("This is the content of the file.")
file.close()
save_button = tk.Button(root, text="Save File", command=save_file)
save_button.pack()
root.mainloop()
Maybe you did not notice, but we changed the initialdir
in the above example to a “.” (dot). This means that the Save Dialog will show up in the same folder as your Python script.
This marks the end of the Tkinter Save File Dialog Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions about the tutorial content can be asked in the comments section below.