Imports and setup
import tkinter as tk
from tkinter import messagebox, ttk
from datetime import datetime
from pathlib import Path
FILE_NAME = "future_letters.txt"
Everything here ships with standard Python. Tkinter draws the window, datetime understands calendar dates, and pathlib checks whether a file exists, so there is nothing extra to install on most computers.
The constant FILE_NAME is the home of every letter: one plain text file that stays on your device after the program closes.
Building the window
class FutureLetterApp:
def __init__(self, root):
self.root = root
self.root.title("Letters to My Future Self")
self.root.geometry("520x480")
self.root.resizable(False, False)
tk.Label(root, text="Write a message to your future self:",
font=("Segoe UI", 11, "bold")).pack(anchor="w", padx=15, pady=(15, 5))
self.message_text = tk.Text(root, height=10, wrap="word", font=("Segoe UI", 10))
self.message_text.pack(fill="x", padx=15)
date_frame = tk.Frame(root)
date_frame.pack(fill="x", padx=15, pady=10)
tk.Label(date_frame, text="Unlock date (YYYY-MM-DD):",
font=("Segoe UI", 10)).pack(side="left")
self.date_var = tk.StringVar()
entry = ttk.Entry(date_frame, textvariable=self.date_var, width=14)
entry.pack(side="left", padx=8)
entry.insert(0, datetime.today().strftime("%Y-%m-%d"))
tk.Button(root, text="Save Letter", command=self.save_letter,
bg="#4CAF50", fg="white", font=("Segoe UI", 10, "bold"),
padx=20, pady=6, relief="flat", cursor="hand2").pack(pady=5)
ttk.Separator(root, orient="horizontal").pack(fill="x", padx=15, pady=10)
tk.Label(root, text="Readable letters (unlocked):",
font=("Segoe UI", 10, "bold")).pack(anchor="w", padx=15)
list_frame = tk.Frame(root)
list_frame.pack(fill="both", expand=True, padx=15, pady=(5, 15))
scrollbar = tk.Scrollbar(list_frame)
scrollbar.pack(side="right", fill="y")
self.letters_box = tk.Listbox(list_frame, font=("Segoe UI", 9),
yscrollcommand=scrollbar.set)
self.letters_box.pack(side="left", fill="both", expand=True)
scrollbar.config(command=self.letters_box.yview)
self.refresh_letters()
This method runs once, when the app starts. It builds the screen from top to bottom: a label, a large text box for your letter, a date field already filled with today, a green save button, and a list with its own scrollbar.
The pack method stacks each widget in order. The button receives the save method without parentheses, which means Tkinter will call it later, only when somebody clicks.
Saving a letter
class FutureLetterApp:
def save_letter(self):
message = self.message_text.get("1.0", "end").strip()
unlock_date = self.date_var.get().strip()
if not message:
messagebox.showwarning("Empty message", "Please write a message first.")
return
try:
datetime.strptime(unlock_date, "%Y-%m-%d")
except ValueError:
messagebox.showerror("Invalid date",
"Please enter the date in YYYY-MM-DD format.")
return
with open(FILE_NAME, "a", encoding="utf-8") as file:
file.write(f"{unlock_date}|{message}\n")
messagebox.showinfo("Saved",
"Your letter is saved. "
"It will be readable after the chosen date.")
self.message_text.delete("1.0", "end")
self.refresh_letters()
We are back inside the same class. Before anything is written, the app checks two things: the letter must not be empty, and the date must really exist. Because strptime reads the calendar, an impossible date such as the thirtieth of February is rejected.
The file is opened in append mode, so a new letter is added to the end and older ones are never erased. A vertical bar separates the date from the message.
Unlocking letters
class FutureLetterApp:
def refresh_letters(self):
self.letters_box.delete(0, "end")
path = Path(FILE_NAME)
if not path.exists():
return
today = datetime.today().date()
with open(FILE_NAME, "r", encoding="utf-8") as file:
for line in file:
line = line.rstrip("\n")
if "|" not in line:
continue
date_str, msg = line.split("|", 1)
try:
unlock = datetime.strptime(date_str, "%Y-%m-%d").date()
except ValueError:
continue
if unlock <= today:
preview = msg if len(msg) <= 60 else msg[:57] + "..."
self.letters_box.insert("end", f"[{date_str}] {preview}")
Here is the heart of the time capsule. The app reads the file line by line and shows a letter only when its date is today or earlier. Letters for the future stay hidden, and long ones are shortened to a neat preview.
Be honest about the lock, though. The text file is not encrypted, so anyone who opens it can read ahead. And each line holds one letter, so if you press Enter inside a message, only its first line will appear in the list. Fixing that makes a great first challenge.
Starting the app
if __name__ == "__main__":
root = tk.Tk()
FutureLetterApp(root)
root.mainloop()
These last lines create the main window, hand it to our class, and start the event loop, which waits patiently for your clicks. Write one letter dated today and another dated next year, and see which one appears.