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
In this example, we will create an application that will save the file using the dialog.
#Import necessary Library from tkinter import * from tkinter.filedialog import asksaveasfile #Create an instance of tkinter window win= Tk() #Set the geometry of tkinter window win.geometry("750x250") #Define the function def save_file(): f = asksaveasfile(initialfile = 'Untitled.txt', defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")]) #Create a button btn= Button(win, text= "Save", command= lambda:save_file()) btn.pack(pady=10) win.mainloop()
In the given code, we have created a "save" button to open the save dialog box using the filedialog module in tkinter.
Click the "Save" button to save the File using the Dialog Box.