WordCounter Pro – Python Tkinter Text Analysis Tool (Full Source Code)
WordCounter Pro – Python Tkinter Text Analysis Tool
WordCounter Pro is a modern desktop application built with Python and Tkinter for advanced text analysis. It provides real-time statistics, keyword density calculation, reading time estimation, file handling, and a clean light/dark mode interface.
🚀 Features
- Live word, character, line, and sentence counting
- Character count with and without spaces
- Reading time estimation (200 WPM)
- Keyword density percentage calculator
- Open, save, and clear text files
- Light & dark mode toggle
- Modern UI using
sv_ttk
🖥️ Requirements
- Python 3.8+
- Tkinter (included with Python)
sv-ttktheme package
pip install sv-ttk
📦 Full Source Code
Below is the complete and ready-to-run Python source code for WordCounter Pro:
import sys
import os
import threading
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import sv_ttk
# =========================
# Helpers
# =========================
def resource_path(file_name):
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, file_name)
def set_status(msg):
status_var.set(msg)
root.update_idletasks()
# =========================
# App Setup
# =========================
root = tk.Tk()
root.title("WordCounter Pro")
root.geometry("1050x650")
# root.iconbitmap(resource_path("logo.ico"))
sv_ttk.set_theme("light")
# =========================
# Globals
# =========================
dark_mode_var = tk.BooleanVar(value=False)
stats_vars = {
"words": tk.StringVar(value="0"),
"characters": tk.StringVar(value="0"),
"characters_no_space": tk.StringVar(value="0"),
"lines": tk.StringVar(value="0"),
"sentences": tk.StringVar(value="0"),
"reading_time": tk.StringVar(value="0 min"),
}
keyword_var = tk.StringVar()
keyword_density_var = tk.StringVar(value="0%")
# =========================
# Theme Toggle
# =========================
def toggle_theme():
style.theme_use("clam")
bg = "#2E2E2E" if dark_mode_var.get() else "#FFFFFF"
fg = "white" if dark_mode_var.get() else "black"
root.configure(bg=bg)
for w in ["TFrame", "TLabel", "TLabelframe", "TLabelframe.Label", "TCheckbutton"]:
style.configure(w, background=bg, foreground=fg)
text_area.configure(
bg="#1e1e1e" if dark_mode_var.get() else "white",
fg="white" if dark_mode_var.get() else "black",
insertbackground="white" if dark_mode_var.get() else "black"
)
set_status(f"Theme switched to {'Dark' if dark_mode_var.get() else 'Light'} mode")
# =========================
# Core Logic
# =========================
def count_text():
content = text_area.get("1.0", tk.END).strip()
if not content:
for key in stats_vars:
stats_vars[key].set("0" if key != "reading_time" else "0 min")
keyword_density_var.set("0%")
return
words_list = content.split()
words = len(words_list)
characters = len(content)
characters_no_space = len(content.replace(" ", "").replace("\n", ""))
lines = len(content.splitlines())
sentences = sum(content.count(x) for x in ".!?")
reading_minutes = max(1, round(words / 200))
stats_vars["reading_time"].set(f"{reading_minutes} min")
stats_vars["words"].set(words)
stats_vars["characters"].set(characters)
stats_vars["characters_no_space"].set(characters_no_space)
stats_vars["lines"].set(lines)
stats_vars["sentences"].set(sentences)
keyword = keyword_var.get().strip().lower()
if keyword:
occurrences = sum(1 for w in words_list if w.lower() == keyword)
density = (occurrences / words) * 100
keyword_density_var.set(f"{density:.2f}%")
else:
keyword_density_var.set("0%")
set_status("Text analyzed")
def delayed_count(event=None):
threading.Timer(0.1, count_text).start()
# =========================
# File Operations
# =========================
def open_file():
path = filedialog.askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not path:
return
with open(path, "r", encoding="utf-8") as f:
text_area.delete("1.0", tk.END)
text_area.insert("1.0", f.read())
count_text()
def save_file():
path = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=[("Text Files", "*.txt")]
)
if not path:
return
with open(path, "w", encoding="utf-8") as f:
f.write(text_area.get("1.0", tk.END))
def clear_text():
if messagebox.askyesno("Clear", "Clear all text?"):
text_area.delete("1.0", tk.END)
count_text()
# =========================
# UI Setup
# =========================
style = ttk.Style()
style.theme_use("clam")
status_var = tk.StringVar(value="Ready")
ttk.Label(root, textvariable=status_var).pack(side=tk.BOTTOM, fill="x")
main = ttk.Frame(root, padding=20)
main.pack(expand=True, fill="both")
ttk.Label(main, text="WordCounter Pro", font=("Segoe UI", 22, "bold")).pack()
ttk.Label(main, text="Advanced word & text analysis tool").pack()
text_area = tk.Text(main, font=("Segoe UI", 11), wrap="word", height=12)
text_area.pack(fill="both", expand=True)
text_area.bind("<KeyRelease>", delayed_count)
root.mainloop()
📌 Conclusion
WordCounter Pro is a practical example of building a professional desktop application using Python and Tkinter. It is ideal for writers, developers, students, and SEO specialists who need quick and accurate text statistics.
Feel free to modify, extend, or package it using PyInstaller for distribution.
Happy coding! 🚀
Download or Explore Useful Tools
Check out more useful tools and downloads here: MateTools on Gumroad



No comments