asksaveasfile() function in Python-Tkinter

by Apr 10, 2020Python Tkinter

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:

Output of the Code
– Click here for the video output

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:

  1. Creating a JSON file with Tkinter Python (with example)
  2. Layout Management using Python Tkinter
  3. Plotting graphs with Matplotlib (Python)

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Data And Classes in Python Tkinter

Data And Classes in Python Tkinter

In this article, we are going to learn about data and classes in python Tkinter. We will save our GUIdata into Tkinter variables after that we will also start using OOP to extend the existing Tkinter classes to extend the built-in functionality of Tkinter....

Python Tkinter MessageBox-Example

Python Tkinter MessageBox-Example

INTRODUCTION In this article, we will learn “How to interface the radio button and messages with Tkinter”. I recommend you to go through this first similarly if you are new to Tkinter go through this. Here we will design a GUI with a 4 radio button and...

Graphical User Interface with tkinter python

Graphical User Interface with tkinter python

In this article, we will tell about the GUI(graphical user interface) in python using. We should also know about how to import the library and using some widgets in it. Let’s have a quick look at the below contents. Contents IntroductionWhat is Tkinter?Packaging...

Plotting graphs with Matplotlib (Python)

Plotting graphs with Matplotlib (Python)

In this tutorial, you'll learn about matplotlib, the plotting library of Python. You'll also get to learn how do we use it for plotting different types of graphs in Python. What is Matplotlib? Matplotlib is an amazing visualization library in Python for 2D plots of...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....