Computer >> Computer tutorials >  >> Programming >> Python

asksaveasfile() function in Python Tkinter


TKinter is a Python module which is used for GUI programming in Python. We create a Canvas and place our UI components with many properties and behaviors in it. In this article, we will see e how to use the ask essay file function to save files created through Python programs into the local drives.

We first create a canvas on which we again place a button using the TTK dot button function. Then declare another function that will use the ask fine to define the file type and save the file into location in the local drive.

Example

from tkinter import *
from tkinter import ttk
from tkinter.filedialog import asksaveasfile

base = Tk()
base.geometry('300x250')

def SaveFile():
   data = [('All tyes(*.*)', '*.*')]
   file = asksaveasfile(filetypes = data, defaultextension = data)

save_btn = ttk.Button(base, text = 'Click to save file ', command = lambda : SaveFile())
save_btn.pack(side = TOP, pady = 20,padx = 50)

mainloop()

Output

Running the above code gives us the following result −

asksaveasfile() function in Python Tkinter

Next on clicking the “click to save file as” button, we get the next window as below.

asksaveasfile() function in Python Tkinter