This project is a basic text editor built with the Tkinter library in Python. It allows you to type, open and edit text files, and save them back to your system. The interface is clean and minimal, using a dark background with light text for comfortable reading and writing.
It is a great practice project for anyone starting with GUI based Python development, especially for understanding how menus, windows and event driven functions work. It also introduces simple file handling by letting the user interact with local text files.
What This Project Includes
The editor supports creating a new document, opening an existing .txt file, making changes, and saving it at any time. Everything runs locally, no internet is required, and the program works on any device that has Python installed.
Project Code Overview
The program is contained in one file named main.py.
Imports
The script uses Tkinter for the GUI and its message box and dialog tools.
import tkinter as tk
from tkinter import messagebox, filedialog
Creating a New File
This function clears whatever text is present inside the editor and prepares a fresh writing area.
def new_file():
text.delete(1.0, tk.END)
Opening an Existing File
The user selects a file from the local system. If a file is chosen, it is opened and the content is displayed inside the editor.
def open_file():
file_path = filedialog.askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if file_path:
with open(file_path, 'r') as file:
content = file.read()
text.delete(1.0, tk.END)
text.insert(tk.END, content)
Saving the Document
This function saves the current editor content. Once saved, the user receives a confirmation alert.
def save_file():
file_path = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if file_path:
with open(file_path, 'w') as file:
content = text.get(1.0, tk.END)
file.write(content)
messagebox.showinfo("Save File", "File saved successfully!")
Setting Up the Window
root = tk.Tk()
root.title("Simple Text Editor")
root.geometry("600x400")
A file menu is added to the top of the window with options for creating new files, opening files, saving and exiting the editor.
file_menu = tk.Menu(root)
root.config(menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
Below the menu is the main text field area where writing takes place. It uses a large font, soft white text and an unobtrusive dark background.
text = tk.Text(
root,
wrap=tk.WORD,
font=("Arial", 20),
fg="white",
bg="black",
insertbackground="white"
)
text.pack(expand=1, fill=tk.BOTH)
root.mainloop()
Running the Application
Save the file as main.py and make sure Python is installed on your machine.
Then run the program using:
python main.py
A window will open immediately, and you can start typing, opening files or saving documents whenever you want.
Possible Enhancements
There is a lot of potential to expand this project further. Features like search functionality, undo and redo, line numbers, syntax highlighting, auto-save and theme selection could be added if you want to develop it into something more advanced.
Final Notes
A text editor may seem like a small tool but it teaches some essential concepts such as graphical interfaces, event handling and file management. It is a useful stepping stone toward larger software like code editors and writing applications, and it gives a practical understanding of how desktop programs are built.