# Save File Dialog Box in Tkinter

TkinterServer Side ProgrammingProgramming

We often use Open and Save Dialog. They are common across many applications and we already know how these dialogs work and behave. For instance, if we click on open, it will open a dialog to traverse the location of the file. Similarly, we have the Save Dialog.

We can create these dialogs using Python tkFileDialog package. In order to work with the package, we have to import this in our environment.

Type the following command to import the tkFileDialog package in the notebook,

from tkinter.filedialog import asksaveasfile

## Example

In this example, we will create an application that will save the file using the dialog.

#Import necessary Library
from tkinter import *

#Create an instance of tkinter window
win= Tk()

#Set the geometry of tkinter window
win.geometry("750x250")

#Define the function
def save_file():
defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])

#Create a button
btn= Button(win, text= "Save", command= lambda:save_file())
win.mainloop()