In this tutorial, we will be learning how to use the asksaveasfile() in Tkinter and save the file in the system. The asksaveasfile() function comes under the class filedialog. The file dialog module provides functions for creating file/directory selection windows.
Let’s imagine you want to make a text editor like notepad. You can make the widgets and menu bars with simple Tkinter knowledge but what if you want to save the file into text format (.txt), askasaveas() helps you here to save the file.
Functions in filedialog modules:
Events:
Directory:
- dirs_double_event – describing the event what to happen when you double click
- dirs_select_event – describing the event what to happen when you select
Files:
- files_double_event – describing the event what to happen when you double click
- files_select_event – describing the event what to happen when you double click
Commands:
- filter_command – filter the arrangement of files.
- cancel_command – terminates the window when it is triggered
Filter:
- get_filter() – retrieve the file filter which is in use.
- set_filter(directory, path) – set the file filter.
Example Code for asksaveasfile()
from tkinter import *
from tkinter.filedialog import asksaveasfile
new = Tk()
new.geometry('640x300')
new.title('IoT4Beginners')
def check():
a = text.get()
file = asksaveasfile(defaultextension=".txt")
file.write(a)
text = Entry(new, font=('TimesNewRoman',20))
button = Button(new,text='Save as',font=('Courier',10), width=30,bd=10, command =check)
text.place(x=10,y=10,height=240,width=610)
button.pack(side=BOTTOM)
mainloop()
Output for Code:
Explanation of the code:
1. Importing modules
from tkinter import *
from tkinter.filedialog import asksaveasfile
I imported everything from Tkinter and the function asksaveasfile() from filedialog module
2. Creating a window
new = Tk()
new.geometry('640x300')
new.title('IoT4Beginners')
Creating a window for the GUI with the size of (640×300) in pixels and naming the title as “IoT4Begineers”
3. Creating widgets
text = Entry(new, font=('TimesNewRoman',20))
button = Button(new,text='Save as',font=('Courier',10), width=30,bd=10, command =check)
I made an Entry box to type the input and a button to trigger function to save the file into text.
4. Creating function to save the file in text.
def check():
a = text.get()
file = asksaveasfile(defaultextension=".txt")
file.write(a)
Here, I got the input of the entry box using get() and saved it in a variable. Later I used saveasfile to save the file, using defaultextension as .txt and write the data from the variable a to the file.
5. Placing the Widgets
text.place(x=10,y=10,height=240,width=610)
button.pack(side=BOTTOM)
mainloop()
Finally, I placed the widgets using geometry matrix functions.
Video Output:
Well, that’s about it. Now go create something more fun, and I hope my tutorial has helped you out somewhat.
To know more about Python Tkinter you can refer the following blogs:
- Creating a JSON file with Tkinter Python (with example)
- Layout Management using Python Tkinter
- Plotting graphs with Matplotlib (Python)