Untitled

mail@pastecode.io avatar
unknown
python
9 days ago
3.4 kB
2
Indexable
Never
import tkinter as tk
import random
import time

class TypingApp():
def __init__(self, master):
self.master = master
master.title("Typing Trainer")

self.texts = open("SentencesLib.txt").readlines()
self.difficulty_levels = ["Easy", "Medium", "Hard"]
self.space_buff=" "
self.moving_sentence=tk.StringVar()
self.current_text_index = random.randint(1,len(self.texts))-1
self.current_difficulty = "Easy"
self.start_time = time.time()
self.end_time = 0
self.errors = 0
self.speed = 30
self.accuracy=0
self.typing_speed=0

self.label_difficulty = tk.Label(master, text="Choose difficulty level:")
self.label_difficulty.pack()

self.difficulty_var = tk.StringVar()
self.difficulty_var.set(self.difficulty_levels[0])
self.difficulty_menu = tk.OptionMenu(
master, self.difficulty_var, *self.difficulty_levels, command=self.change_difficulty)
self.difficulty_menu.pack()

self.label = tk.Label(master, text="Type the following text:")
self.label.pack()

self.moving_sentence.set(self.space_buff+self.texts[self.current_text_index])
self.text_display = tk.Label(master, anchor="w", width=120,textvariable=self.moving_sentence)
self.text_display.pack()

self.text_input = tk.Text(master, width=60, height=6) #tk.Entry(master)
self.text_input.pack()

self.feedback = tk.Label(master, text="")
self.feedback.pack()

self.text_input.bind("<Return>", self.check_text)

self.start = tk.Button(master, text = "Start", command=self.updateDisplay)
self.start.pack(side='left', anchor='e', expand=True)
self.exit = tk.Button(master, text = "Exit", command=self.close_window)
self.exit.pack(side='right', anchor='w', expand=True)
def close_window(self):
root.destroy()

def updateDisplay(self):
self.start_time = time.time()
self.update()
def update(self):
current_label=self.moving_sentence.get()
self.moving_sentence.set(current_label[1:])
root.after(5*self.speed, self.update)


def change_difficulty(self, event):
self.current_difficulty = self.difficulty_var.get()
self.update_speed()

def update_speed(self):
if self.current_difficulty == "Easy":
self.speed = 30
elif self.current_difficulty == "Medium":
self.speed = 20
else:
self.speed = 10

self.current_text_index = random.randint(1,len(self.texts))-1
self.moving_sentence.set(self.space_buff+self.texts[self.current_text_index])
#self.text_display.config(text=self.texts[self.current_text_index])
self.text_input.delete("1.0", tk.END)
self.start_time = time.time()
self.feedback.config(text=f"")

def get_accuracy(self, type_sentence, target_sentence):
correct_num=0
string_len=len(target_sentence)
ref_len=string_len
if len(type_sentence)<string_len :
string_len=len(type_sentence)
for x in range(string_len):
if type_sentence[x]==target_sentence[x]:
correct_num+=1
self.accuracy=100.0*correct_num/ref_len

def get_typingSpeed(self, type_sentence):
typing_time=(self.end_time-self.start_time)/60
self.typing_speed=len(type_sentence)/typing_time

def check_text(self, event):
self.end_time = time.time()
text_typed = self.text_input.get("1.0", "end-1c")
text_to_match = self.texts[self.current_text_index]

if len(text_typed)>0:
self.get_typingSpeed(text_typed)
self.get_accuracy(text_typed, text_to_match)

self.feedback.config(text=f"Your typing Speed is {self.typing_speed:.2f} characters per minute \n Your typing accuracy is {self.accuracy:.1f}%")

root = tk.Tk()
root.geometry("800x300")
app = TypingApp(root)
root.mainloop()
Leave a Comment