Program Code
Program Code
import tkinter as tk
import time
import random
PROMPTS = [
"The quick brown fox jumps over the lazy dog. This
sentence contains every letter in the English alphabet,
making it a great practice tool.",
"Typing fast requires a lot of practice and patience. The
more you type, the better you become at both speed and
accuracy.",
"Python programming is fun and powerful. With practice,
you can build applications, automate tasks, and even
develop artificial intelligence models.",
"Speed and accuracy are both important in typing. It is
not just about typing fast, but also about making as few
mistakes as possible.",
"A good typist focuses on precision first, and then builds
speed over time. Consistent practice with challenging
texts helps improve performance."
]
class TypingTester:
def __init__(self, root):
self.root = root
self.root.title("Typing Speed Tester")
self.root.geometry("800x500")
self.show_welcome_screen()
def show_welcome_screen(self):
self.clear_screen()
self.label_welcome = tk.Label(
self.root, text="Welcome to Typing Speed Tester",
font=("Arial", 28, "bold"),
bg="#E3F2FD", fg="#0D47A1"
)
self.label_welcome.pack(pady=20)
self.label_subtitle = tk.Label(
self.root, text="Improve your typing speed and
accuracy with our test!",
font=("Arial", 16),
bg="#E3F2FD", fg="#1565C0"
)
self.label_subtitle.pack(pady=10)
self.button_start = tk.Button(
self.root, text="Start Typing Test",
command=self.start_typing_test,
font=("Arial", 16, "bold"),
bg="#64B5F6", fg="white",
padx=15, pady=10,
relief="raised", bd=3
)
self.button_start.pack(pady=20)
self.label_footer = tk.Label(
self.root, text="Get ready to type fast and
accurately!",
font=("Arial", 14, "italic"),
bg="#E3F2FD", fg="#1E88E5"
)
self.label_footer.pack(pady=10)
def start_typing_test(self):
"""Starts the typing test after clicking start."""
self.reset_test()
self.label_prompt = tk.Label(self.root,
text=self.prompt_text, font=("Arial", 14), wraplength=750,
justify="left")
self.label_prompt.pack(pady=10)
self.button_submit = tk.Button(self.root,
text="Submit", command=self.calculate_results,
font=("Arial", 12))
self.button_submit.pack(pady=5)
def reset_test(self):
"""Resets the typing test with a new prompt."""
self.clear_screen()
self.prompt_text = random.choice(PROMPTS)
self.start_time = None
if typed_text == correct_text:
self.entry_text.config(fg="green")
else:
self.entry_text.config(fg="red")
def calculate_results(self):
"""Calculates WPM and accuracy when the user
submits the text."""
if self.start_time is None:
self.label_result.config(text="Start typing first!",
fg="red")
return
end_time = time.time()
elapsed_time = end_time - self.start_time
typed_text = self.entry_text.get("1.0", tk.END).strip()
num_words = len(typed_text.split())
wpm = (num_words / elapsed_time) * 60 if
elapsed_time > 0 else 0
self.label_result.config(text=f"WPM:
{wpm:.2f}\nAccuracy: {accuracy:.2f}%\nTime:
{elapsed_time:.2f} sec", fg="blue")
def clear_screen(self):
"""Clears all widgets from the window."""
for widget in self.root.winfo_children():
widget.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = TypingTester(root)
root.mainloop()