How to Save Text to a File Using Python Tkinter

You typed notes into a Tkinter window, closed it, and they’re gone. Nothing wrote them to disk.

Quick answer: to make Tkinter save text to file, read the Text widget with text.get("1.0", "end-1c") and write that string with open(path, "w") inside a button callback:

with open(path, "w", encoding="utf-8") as f:
    f.write(text.get("1.0", "end-1c"))

In this tutorial, we’ll build an order-notes window on Python 3.12.5 with Tk 8.6, add a Save As dialog and append to a log.

What You Need Before You Start

  • Python 3 with Tkinter (the Windows installer includes it).
  • A folder to write into, here C:\pyguides\notes.
  • A button callback. See Tkinter Button commands.

Step 1: Save the Tkinter Text widget to a file

Our sample is a notes window for Harbor Lane Coffee:

import tkinter as tk

NOTES = r"C:\pyguides\notes\order_notes.txt"

root = tk.Tk()
root.title("Harbor Lane Coffee - Order notes")
root.geometry("580x300")

text = tk.Text(root, height=7, font=("Consolas", 11))
text.pack(fill="both", expand=True, padx=12, pady=(12, 6))
status = tk.Label(root, text="Not saved yet", anchor="w")

def save_notes():
    content = text.get("1.0", "end-1c")
    with open(NOTES, "w", encoding="utf-8") as f:
        f.write(content)
    lines = content.count("\n") + 1
    status.config(text=f"Saved {lines} lines to order_notes.txt")

save = tk.Button(root, text="Save notes", command=save_notes)
save.pack(side="left", padx=12, pady=(0, 12))
status.pack(side="left", pady=(0, 12))
root.mainloop()
Tkinter Order notes window with four lines for order 2048 in a Text widget, a Save notes button and the status Saved 4 lines to order_notes.txt
After Save notes runs, the status label confirms that 4 lines went to order_notes.txt.

"1.0" means line 1, character 0. "end-1c" skips the newline Tk always adds. The with open() statement closes the file even if write() fails.

Step 2: Check what Tkinter wrote to the file

We read the file back in Command Prompt:

from pathlib import Path

notes = Path(r"C:\pyguides\notes\order_notes.txt")
print(notes.read_text(encoding="utf-8"))
print("Size on disk:", notes.stat().st_size, "bytes")

Output:

Order #2048 - Dana Whitfield
2 x Oat latte, 1 x Blueberry muffin
Total: $14.75
Pickup: Nov 6, 2026 at 8:30 AM
Size on disk: 112 bytes
Command Prompt running read_notes.py: the four order note lines, then Size on disk 112 bytes
The file holds the four lines with no blank line at the end; Windows stores each line break as two bytes, so it reads 112 bytes.

Loading it back into a widget is in reading a text file in Tkinter.

Does asksaveasfilename save the file for you?

No. filedialog.asksaveasfilename() only returns a path. You write the file:

from tkinter import filedialog

def save_as():
    path = filedialog.asksaveasfilename(
        initialdir=r"C:\pyguides\notes",
        defaultextension=".txt",
        filetypes=[("Text files", "*.txt"), ("All files", "*.*")],
    )
    if not path:              # Cancel returns an empty string
        status.config(text="Save canceled")
        return
    with open(path, "w", encoding="utf-8") as f:
        f.write(text.get("1.0", "end-1c"))
    status.config(text=f"Saved to {path}")

We tested both branches: a picked path saved the text, and Cancel (an empty string) set the label to “Save canceled”.

defaultextension adds .txt when it’s missing. Other dialogs are in the Tkinter filedialog guide.

Step 3: Append Entry text to a log file

Same shop, now a call log. Mode "a" adds a line instead of replacing the file:

import tkinter as tk

LOG = r"C:\pyguides\notes\call_log.txt"

root = tk.Tk()
root.title("Harbor Lane Coffee - Call log")
root.geometry("580x170")

tk.Label(root, text="Call note:").pack(anchor="w", padx=12, pady=(14, 2))
entry = tk.Entry(root, font=("Segoe UI", 11))
entry.pack(fill="x", padx=12)
status = tk.Label(root, text="", anchor="w")

def add_note():
    note = entry.get().strip()
    with open(LOG, "a", encoding="utf-8") as f:
        f.write(note + "\n")
    entry.delete(0, "end")
    status.config(text=f"Added: {note}")

add = tk.Button(root, text="Add to log", command=add_note)
add.pack(side="left", padx=12, pady=12)
status.pack(side="left")
root.mainloop()
Tkinter Call log window with an empty Entry, an Add to log button and the status Added: Refunded $4.25 for a missing muffin
Two notes were added; the status shows the last one and the Entry is cleared for the next call.

After two clicks, call_log.txt held both lines:

Dana Whitfield moved pickup to 9:00 AM
Refunded $4.25 for a missing muffin

entry.get() needs no index arguments. More in getting Tkinter Entry text.

Tkinter save-to-file cheat sheet

GoalCodeNote
Read all Text contenttext.get("1.0", "end-1c")Skips Tk’s extra newline
Replace the fileopen(path, "w")Old content is lost
Add to the fileopen(path, "a")Creates it if missing
Ask for a pathfiledialog.asksaveasfilename()Returns “” on Cancel

Common Mistakes to Avoid When Saving Tkinter Text

The most common one is reading to "end". In our test, "end" returned 'Total: $14.75\n', with a trailing newline, while "end-1c" returned 'Total: $14.75'.

Two more we’ve hit: a relative path like "notes.txt" lands in the current folder, and a missing folder raises FileNotFoundError. Create it first, as in creating a directory in Python.

Important: read the widget inside the callback. After mainloop() returns, get() raises TclError.

Next Steps

Add an Open button and a menu, and you’ve got the Python Tkinter text editor.

We’d keep these nearby for your next Tkinter screen:

Frequently asked questions

How do I save text from a Tkinter Text widget to a file?

Call text.get("1.0", "end-1c") and write it with open(path, "w").

Why does my saved file have an extra blank line?

get("1.0", "end") includes Tk’s trailing newline. Use "end-1c" instead.

How do I add text to a file without overwriting it?

Open it in append mode, open(path, "a").

What’s the difference between asksaveasfilename and asksaveasfile?

asksaveasfilename returns a path string; asksaveasfile returns an open file object.

How do I know if the user canceled the Save dialog?

asksaveasfilename returns an empty string on Cancel.

Where are the file dialog options documented?

See Tkinter dialogs in the Python docs.