Notepad-style text editor
A simple text editor with open, save, find and replace, word count and an unsaved-changes warning.
What you'll learn
- âMenus and dialogs in Tkinter
- âFile handling with encodings
- âText widget tags and search
What you'll build
- âNew, Open, Save, Save As
- âFind and replace with highlights
- âWord and line count in a status bar
- âWarns before closing unsaved work
Step-by-step guide
- 1
Build the layout
A Text widget with a Scrollbar, a Menu bar (File, Edit, View) and a Label at the bottom as status bar.
- 2
Open and save files
Use filedialog.askopenfilename / asksaveasfilename and open files with encoding="utf-8".
- 3
Track changes
Bind <<Modified>> to mark the document dirty and show "*" in the window title.
- 4
Find and replace
Use text.search(pattern, start, stopindex="end") in a loop, tag matches with text.tag_add("found", ...) and highlight with tag_config.
- 5
Status bar
On every key release, count words with len(text.get("1.0", "end").split()) and show line:column from text.index("insert").
- 6
Safe close
root.protocol("WM_DELETE_WINDOW", on_close) and ask "Save changes?" with messagebox.askyesnocancel.
- 7
Shortcuts
Bind Ctrl+S, Ctrl+O, Ctrl+F and Ctrl+N.
đ Make it stand out
- +Tabs for multiple files with ttk.Notebook
- +Basic Python syntax highlighting with tags
- +Auto-save every minute