Skip to content

Commit 1c5040c

Browse files
authored
Create Vowel-and-Consonant-Counter.py
1 parent a8b9639 commit 1c5040c

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import sys
2+
import os
3+
import tkinter as tk
4+
from tkinter import ttk, messagebox
5+
6+
# =========================
7+
# THEME
8+
# =========================
9+
APP_BG = "#121212"
10+
PANEL_BG = "#1F1F1F"
11+
BTN_BG = "#2C2C2C"
12+
ACCENT = "#FF6F61"
13+
TEXT_CLR = "#E0E0E0"
14+
15+
FONT = ("Segoe UI", 11)
16+
17+
# =========================
18+
# RESOURCE PATH
19+
# =========================
20+
def resource_path(file_name):
21+
base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
22+
return os.path.join(base_path, file_name)
23+
24+
# =========================
25+
# TEXT LOGIC
26+
# =========================
27+
VOWELS = set("aeiouAEIOU")
28+
29+
def count_chars(text):
30+
vowels = sum(1 for c in text if c in VOWELS)
31+
consonants = sum(1 for c in text if c.isalpha() and c not in VOWELS)
32+
total_letters = vowels + consonants
33+
total_chars = len(text)
34+
words = len(text.split())
35+
36+
return vowels, consonants, total_letters, words, total_chars
37+
38+
# =========================
39+
# APP
40+
# =========================
41+
class VowelConsonantCounter:
42+
def __init__(self, root):
43+
self.root = root
44+
root.title("Vowel & Consonant Counter")
45+
root.geometry("1000x520")
46+
root.configure(bg=APP_BG)
47+
root.resizable(False, False)
48+
49+
# =========================
50+
# LEFT PANEL
51+
# =========================
52+
left = tk.Frame(root, bg=PANEL_BG, width=420)
53+
left.pack(side="left", fill="y")
54+
55+
tk.Label(
56+
left,
57+
text="Enter Text",
58+
bg=PANEL_BG,
59+
fg=TEXT_CLR,
60+
font=("Segoe UI", 12, "bold")
61+
).pack(anchor="w", padx=12, pady=(12, 4))
62+
63+
self.text_box = tk.Text(
64+
left,
65+
height=17,
66+
bg=BTN_BG,
67+
fg=TEXT_CLR,
68+
font=FONT,
69+
wrap="word",
70+
padx=10,
71+
pady=10,
72+
relief="flat"
73+
)
74+
self.text_box.pack(fill="x", padx=12)
75+
self.text_box.bind("<KeyRelease>", self.update_counts)
76+
77+
# =========================
78+
# BUTTONS
79+
# =========================
80+
btn_frame = tk.Frame(left, bg=PANEL_BG)
81+
btn_frame.pack(fill="x", padx=12, pady=12)
82+
83+
def make_btn(text, cmd, color=BTN_BG):
84+
return tk.Button(
85+
btn_frame,
86+
text=text,
87+
command=cmd,
88+
bg=color,
89+
fg="white",
90+
font=("Segoe UI", 11, "bold"),
91+
relief="flat",
92+
height=2,
93+
width=20
94+
)
95+
96+
make_btn("Clear Text", self.clear_text).pack(side="left", expand=True, padx=4)
97+
make_btn("About", self.show_about, ACCENT).pack(side="left", expand=True, padx=4)
98+
99+
# =========================
100+
# RIGHT PANEL
101+
# =========================
102+
right = tk.Frame(root, bg=APP_BG)
103+
right.pack(side="right", fill="both", expand=True)
104+
105+
stats_card = tk.Frame(right, bg=PANEL_BG)
106+
stats_card.pack(padx=30, pady=40, fill="both", expand=True)
107+
108+
tk.Label(
109+
stats_card,
110+
text="Text Statistics",
111+
bg=PANEL_BG,
112+
fg=TEXT_CLR,
113+
font=("Segoe UI", 14, "bold")
114+
).pack(pady=(20, 10))
115+
116+
self.stats = {}
117+
for label in ["Vowels", "Consonants", "Total Letters", "Words", "Characters"]:
118+
frame = tk.Frame(stats_card, bg=PANEL_BG)
119+
frame.pack(fill="x", padx=40, pady=6)
120+
121+
tk.Label(
122+
frame,
123+
text=label,
124+
bg=PANEL_BG,
125+
fg="#AAAAAA",
126+
font=("Segoe UI", 11)
127+
).pack(side="left")
128+
129+
value = tk.Label(
130+
frame,
131+
text="0",
132+
bg=PANEL_BG,
133+
fg="white",
134+
font=("Segoe UI", 12, "bold")
135+
)
136+
value.pack(side="right")
137+
self.stats[label] = value
138+
139+
# =========================
140+
# METHODS
141+
# =========================
142+
def update_counts(self, event=None):
143+
text = self.text_box.get("1.0", "end-1c")
144+
145+
v, c, letters, words, chars = count_chars(text)
146+
147+
self.stats["Vowels"].config(text=str(v))
148+
self.stats["Consonants"].config(text=str(c))
149+
self.stats["Total Letters"].config(text=str(letters))
150+
self.stats["Words"].config(text=str(words))
151+
self.stats["Characters"].config(text=str(chars))
152+
153+
def clear_text(self):
154+
self.text_box.delete("1.0", "end")
155+
self.update_counts()
156+
157+
def show_about(self):
158+
messagebox.showinfo(
159+
"About",
160+
"Vowel & Consonant Counter\n\n"
161+
"Counts vowels, consonants, words, and characters.\n\n"
162+
"Built by MateTools"
163+
)
164+
165+
# =========================
166+
# RUN
167+
# =========================
168+
if __name__ == "__main__":
169+
root = tk.Tk()
170+
VowelConsonantCounter(root)
171+
root.mainloop()

0 commit comments

Comments
 (0)