|
| 1 | +import tkinter as tk |
| 2 | +import random |
| 3 | +import timeit |
| 4 | + |
| 5 | +start_time = 0 |
| 6 | + |
| 7 | +def get_sentence(): |
| 8 | + global sentence,sentence_length,sentence_words |
| 9 | + Reset() |
| 10 | + with open("./Typing-Speed-Test/sentences.txt","r") as f: |
| 11 | + sentences = f.readlines() |
| 12 | + sentence = random.choice(sentences).rstrip() |
| 13 | + sentence_label.config(text = sentence) |
| 14 | + sentence_length = len(sentence) |
| 15 | + sentence_words = len(sentence.split()) |
| 16 | + print(sentence_length) |
| 17 | + |
| 18 | +def result(): |
| 19 | + time_taken = round(timeit.default_timer() - start_time) |
| 20 | + typed_text = text.get() |
| 21 | + wpm = round((sentence_words/time_taken)*60) |
| 22 | + count = 0 |
| 23 | + for index,char in enumerate(typed_text): |
| 24 | + if sentence[index] == char: |
| 25 | + count+=1 |
| 26 | + accu = round((count/sentence_length)*100) |
| 27 | + |
| 28 | + Speed.config(text = f"Speed: {wpm} WPM") |
| 29 | + Accuracy.config(text = f"Accuracy: {accu}") |
| 30 | + Time.config(text = f"Time: {time_taken} sec") |
| 31 | + |
| 32 | + |
| 33 | +def check(text): |
| 34 | + global start_time |
| 35 | + if start_time==0 and len(text.get())==1: |
| 36 | + start_time = timeit.default_timer() |
| 37 | + elif len(text.get()) == sentence_length: |
| 38 | + typing_box.config(state = tk.DISABLED) |
| 39 | + result() |
| 40 | + |
| 41 | +def Reset(): |
| 42 | + global start_time |
| 43 | + typing_box.config(state = tk.NORMAL) |
| 44 | + typing_box.delete(0,tk.END) |
| 45 | + start_time=0 |
| 46 | + |
| 47 | + Speed.config(text = f"Speed: 00 WPM") |
| 48 | + Accuracy.config(text = f"Accuracy: 00") |
| 49 | + Time.config(text = f"Time: 0 sec") |
| 50 | + |
| 51 | + |
| 52 | +window = tk.Tk() |
| 53 | +window.geometry("900x600+300+100") |
| 54 | +window.title("Typing Speed Test") |
| 55 | +bg_color = "#00154D" |
| 56 | +window.config(bg = bg_color) |
| 57 | + |
| 58 | +tk.Label(window,text="Typing Speed Test", anchor = tk.CENTER,font = ("times new roman",50,"bold"),bg=bg_color,fg = "#F2BC90").pack(side=tk.TOP) |
| 59 | + |
| 60 | +sentence_label = tk.Label(window,text="a set of words that is complete in itself, typically containing a subject and predicate, conveying a statement, question, exclamation, or command, and consisting of a main clause and sometimes one or more subordinate clauses.",wraplength=700,anchor = tk.CENTER,font = ("arial",20,"bold"),bg=bg_color,fg="#ffffff",width=40,justify=tk.LEFT) |
| 61 | +sentence_label.pack(pady=40) |
| 62 | +text = tk.StringVar() |
| 63 | +text.trace("w",lambda name,index,mode,text=text: check(text)) |
| 64 | +typing_box = tk.Entry(window,font = ("arial",20,"bold"),width=40,textvariable = text) |
| 65 | +typing_box.place(x=150,y=360) |
| 66 | + |
| 67 | +reset_button = tk.Button(window,text = "Reset" ,font = ("arial",18,"bold"),width=12,command= Reset) |
| 68 | +reset_button.place(x=120,y=450) |
| 69 | +change_button = tk.Button(window,text = "Change Text" ,font = ("arial",18,"bold"),width=12,command = get_sentence) |
| 70 | +change_button.place(x=360,y=450) |
| 71 | +result_button = tk.Button(window,text = "Result" ,font = ("arial",18,"bold"),width=12,command =result) |
| 72 | +result_button.place(x=600,y=450) |
| 73 | + |
| 74 | +Speed = tk.Label(window,text = "Speed: 00 WPM",font = ("arial",15,"bold"),bg=bg_color,fg="#ffffff") |
| 75 | +Speed.place(x=120,y=530) |
| 76 | +Accuracy = tk.Label(window,text = "Accuracy: 00",font = ("arial",15,"bold"),bg=bg_color,fg="#ffffff") |
| 77 | +Accuracy.place(x=380,y=530) |
| 78 | +Time = tk.Label(window,text = "Time: 0 sec",font = ("arial",15,"bold"),bg=bg_color,fg="#ffffff") |
| 79 | +Time.place(x=620,y=530) |
| 80 | + |
| 81 | +get_sentence() |
| 82 | +window.mainloop() |
0 commit comments