Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python Tkinter GUI Script to Save Data to a Text File Full Project For Beginners

Posted on November 6, 2022

 

 

 

Welcome folks today in this blog post we will be looking how to save data to a text file in tkinter. All the full source code of the application will be shown below.

 

 

Get Started

 

 

In order to get started you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import asksaveasfile
from tkinter import END
 
my_w = tk.Tk()
my_w.geometry("400x300")  # Size of the window
my_w.title('Python tkinter save data to text file')
 
my_w.mainloop()  # Keep the window open

 

 

As you can see we are importing the tkinter library and also the methods required for displaying the popup window to save the data inside the text file. And for this we are importing the fileDialog class and from that we are importing the asksaveasfile module. And then we are initializing the new tkinter window and setting the geometry of the window such as width and height and also the title of the window and then we are starting the tkinter application by calling the mainloop() method.

 

 

 

 

Now we need to add the label widget inside the tkinter window as shown below. We will be setting the font size also of the text.

 

 

Python
1
2
3
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='My Note Book',width=30,font=my_font1)  
l1.grid(row=1,column=1)

 

 

 

As you can see we are setting the times font and giving the size and also the bold font. And then we are adding the label widget here giving the width of the label and also set the font of the label. And then we are adding the label in the grid manner.

 

 

 

 

Now we need to add the textarea widget inside the tkinter window and again we will be adding that in a grid manner as shown below

 

 

Python
1
2
t1 = tk.Text(my_w,  width=40, height=3)
t1.grid(row=2,column=1)

 

 

Here we are using the text() method to add the text widget inside the tkinter window. Here we are providing the width and height and then we are adding the widget using the grid method

 

Now we will be adding the button widget inside the tkinter window. Where we have the label of save file. This button is useful for saving the text which is written inside the textarea to an actual text file.

 

 

Python
1
2
b1 = tk.Button(my_w, text='Save',command=lambda:save_file(), width=20)
b1.grid(row=4,column=1)

 

 

As you can see we are using the button widget to display the button and then we have added the command which is actually a function called save_file() which is called whenever the button is clicked. And then we are adding the button to the grid.

 

 

 

 

 

Now we need to define the function when the button will be clicked to save the data to the text file. The code of the function is shown below

 

 

Papyrus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def save_file():
    my_str1=t1.get("1.0",END)  # read from one text box t1
    fob=filedialog.asksaveasfile(filetypes=[('text file','*.txt')],
        defaultextension='.txt',initialdir='D:\\my_data\\my_html',
        mode='w')
    try:
        fob.write(my_str1)
        fob.close()
        t1.delete('1.0',END) # Delete from position 0 till end
        t1.update()  
        b1.config(text="Saved")
        b1.after(3000, lambda: b1.config(text='Save'))
    except :
        print (" There is an error...")

 

 

As you can see in the above function we are first of all getting the input data which is written inside the textarea widget and storing it inside the variable and then we are showing the popup window to save the file using the asksaveasfile() method and here we are pasisng the actual text to be saved and in the second argument we are passing the extension of the file which in this case is .txt for text files. And then also we are providing the path or location where we need to save the file. We are providing the initial directory variable and then we are attaching the mode to w to actually write the document.

 

 

 

 

 

 

 

Full Source Code

 

 

Wrapping the blog post this is the full source code of the app.py file

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import asksaveasfile
from tkinter import END
 
my_w = tk.Tk()
my_w.geometry("400x300")  # Size of the window
my_w.title('Python tkinter save data to text file')
 
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='My Note Book',width=30,font=my_font1)  
l1.grid(row=1,column=1)
 
t1 = tk.Text(my_w,  width=40, height=3)
t1.grid(row=2,column=1)
 
b1 = tk.Button(my_w, text='Save',command=lambda:save_file(), width=20)
b1.grid(row=4,column=1)
 
def save_file():
    my_str1=t1.get("1.0",END)  # read from one text box t1
    fob=filedialog.asksaveasfile(filetypes=[('text file','*.txt')],
        defaultextension='.txt',initialdir='D:\\my_data\\my_html',
        mode='w')
    try:
        fob.write(my_str1)
        fob.close()
        t1.delete('1.0',END) # Delete from position 0 till end
        t1.update()  
        b1.config(text="Saved")
        b1.after(3000, lambda: b1.config(text='Save'))
    except :
        print (" There is an error...")
 
my_w.mainloop()  # Keep the window open

Recent Posts

  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Android Java Project to Download Random Image From Unsplash Using OkHttp & Picasso Library & Display it
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme