Text Editor App in Python
This project is a Simple Text Editor built using Python’s Tkinter GUI library, demonstrating
file handling, menu creation, and event-driven programming.
📌 Important Notes and Terms
🔹 Tkinter
Tkinter is Python’s built-in GUI (Graphical User Interface) library used to
create desktop applications with windows, buttons, menus, and text boxes.
🔹 Module
A module is a Python file that contains ready-made code.
Example:
import tkinter as tk
Here, tkinter is a module.
🔹 Submodule
A submodule is a smaller part inside a module that provides specific features.
Example:
from tkinter import filedialog, messagebox
● filedialog → file open/save windows
● messagebox → popup messages
🔹 [Link]()
Creates the main application window.
Without this, no GUI can exist.
🔹 [Link]()
Sets the title text shown on the window’s top bar.
🔹 [Link]("800x600")
Sets the window size
● 800 → width
● 600 → height
🔹 Text Widget
[Link]()
Creates a multi-line text editor where users can type, edit, and view text.
🔹 wrap=[Link]
Ensures text wraps by words, not by characters.
Improves readability and looks professional.
🔹 pack(expand=True, fill=[Link])
● expand=True → widget grows with window
● fill=[Link] → fills width and height
🔹 Function
A function is a reusable block of code that runs only when called.
Example:
def open_file():
🔹 [Link](1.0, [Link])
Deletes all text from the text editor.
● 1.0 → beginning
● [Link] → end
🔹 [Link]([Link], data)
Inserts text at the end of the editor.
Used to safely load file content.
🔹 with open(...)
Used for safe file handling.
Automatically:
● Opens the file
● Closes the file after use
🔹 File Modes
● "r" → read file
● "w" → write file (overwrites)
🔹 File Dialog
[Link]()
Opens system file browser to select a file.
[Link]()
Opens system window to save a file.
🔹 Message Box
[Link]()
Displays popup messages for success, info, or alerts.
🔹 Menu Bar
A menu bar is the top bar containing options like:
● File
● Edit
● Help
Created using:
[Link]()
🔹 Submenu
A submenu is a dropdown inside a menu.
Example:
File → New, Open, Save, Exit
🔹 add_command()
Links a menu option to a function.
🔹 [Link]
Closes the application safely.
🔹 Event-Driven Programming
The program waits for user actions (clicks, typing).
Functions run only when events occur.
🔹 [Link]()
Starts the event loop.
● Keeps the window open
● Listens for user actions
● Ends when window is closed
Full Code
# Import tkinter module for creating GUI applications
import tkinter as tk
# Import submodule filedialog for open/save dialogs and submodule
messagebox for popups
from tkinter import filedialog, messagebox
# -------------------- MAIN WINDOW --------------------
# Create main application window
root = [Link]()
# Set window title
[Link]("Simple Text Editor")
# Set window size
[Link]("800x600")
# -------------------- TEXT EDITOR AREA --------------------
# Create text editor area
text = [Link](
root, # Parent window
wrap=[Link], # Wrap text by words (not characters)
font=("Helvetica", 12) # Font style and size
)
# Make text area fill the entire window
[Link](expand=True, fill=[Link])
# -------------------- FILE FUNCTIONS --------------------
# Function to create a new file (clear all text)
def new_file():
# Delete all text from the text box (from start to end)
[Link](1.0, [Link])
# Function to open an existing text file
def open_file():
# Open file dialog to select a text file
file_path = [Link](
defaultextension=".txt", # Default file extension
filetypes=[("Text Files", "*.txt")] # Allow only .txt files
)
# Check if a file is selected
if file_path:
# Open the selected file in read mode
with open(file_path, "r") as file:
# Clear old text from editor
[Link](1.0, [Link])
# Insert file content into the text editor
[Link]([Link], [Link]())
# Function to save the current text to a file
def save_file():
# Open save file dialog
file_path = [Link](
defaultextension=".txt", # Default file extension
filetypes=[("Text Files", "*.txt")] # Allow only .txt files
)
# Check if file path is selected
if file_path:
# Open file in write mode
with open(file_path, "w") as file:
# Write text editor content into file
[Link]([Link](1.0, [Link]))
# Show success message after saving file
[Link]("Info", "File saved successfully!")
# -------------------- MENU BAR --------------------
# Create menu bar
menu = [Link](root)
# Attach menu bar to the window
[Link](menu=menu)
# Create File menu
# Dropdown under File which has options like New, Open, Save, Exit
file_menu = [Link](menu)
# Add File menu to menu bar
menu.add_cascade(label="File", menu=file_menu)
# Add New option to File menu
file_menu.add_command(label="New", command=new_file)
# Add Open option to File menu
file_menu.add_command(label="Open", command=open_file)
# Add Save option to File menu
file_menu.add_command(label="Save", command=save_file)
# Add a separator line in menu
file_menu.add_separator()
# Add Exit option to close the application
file_menu.add_command(label="Exit", command=[Link])
# -------------------- RUN APPLICATION --------------------
# Run the application continuously
# Starts and keeps the window open
[Link]()