Do you know you can test your typing speed using Python? A typing speed tester is a fun way to test how fast someone can type. It can be used to design a game-like interface to allow people of all ages to find out how fast they can type, or it can be used in an educational environment where typing speed is vital to a student’s success. In this blog post, we will cover the steps to creating a typing speed tester to find the typing speed of a single user. we will be using Tkinter to create a graphical interface for the app.

Prerequisites

Tkinter:

Tkinter is the standard in-built Python library for creating graphical user interfaces (GUIs). It’s not really a separate “thing” – it’s just the part of the standard library that’s responsible for drawing graphics, accepting user input, etc. Tkinter is designed to be easy to use, and thus it’s a good library for beginners who are just starting out with Python or GUI development. 

Random:

The random Python module is an in-built module of Python which may be used to tinker with and generate unpredictable numbers. The module may also be used to perform a variety of tricks such as generating unexpected figures for lists or strings, etc. we are using this so that each time someone resets the app, a different phrase will appear.

Threading:

This package is used to introduce multiple threading in the program. When a program is executed, it is viewed as a process and inside a process, there is a thread. When a program is run synchronously, it creates one process under which there is one thread. But when there’s an asynchronous program there is one process but there is more than one thread.

For example, in this tutorial, we are using a threading module to run two tasks at the same time. Task one is to register a user typing input and task two is to keep track of how much time has elapsed as the user types. We can do that by creating multiple threads using the module.

Step 1: Create a text file

Create a text.txt file and save a random line of sentence that you want to show on the typing tester.

Hello, how are you, I guess you are fine hmm.

please don’t forget to share your thoughts.

Share this article with your friends.

Hello, the world this text is a pretty useful program.

python is an awesome programming language.

Step 2: Create a new .py file

Now follow along with the code

Step 3: Import dependencies

first, let’s import the dependencies

import tkinter as tk
import time
import threading
import random

Step 4: Define the graphical interface of the tester

The actual graphical interface will be part of a class because when you have a graphical user interface with functions and UI elements that access these functions and UI elements as well for that you want this to be in one object otherwise you will be confused about which one to define first.

Here in the application, we want to have four things

  1. Label where the sentence will be shown to types by the user
  2. A text box to type in by the user
  3. Timer to show CPS (Character per second), CPM (Character per minute), WPS (Word per second), and WPM (Word per minute)
  4. And in the last, we want to have a reset button that will reset the application and show the different sentences to type.
# create a class for ul elements
class TypeSpeedGUI:

    # create a simple constructor
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Typing speed tester")
        self.root.geometry("800x600")

        self.text = open("text.txt", "r").read().split("\n")

        self.frame = tk.Frame(self.root)

        # creating a lable
        self.sample_label = tk.Label(self.frame, text=random.choice(self.text), font=("Helventica", 18))
        self.sample_label.grid(row=0, column=0, columnspan=2, padx=5, pady=5)

        # creating a text box
        self.input_entry = tk.Entry(self.frame, width=40, font=("Helventica", 24))
        self.input_entry.grid(row=1, column=0, columnspan=2, padx=5, pady=10)
        # adding the function to start automatically if the key is pressed
        self.input_entry.bind("<KeyPress>", self.start)

        # creating a label for the timer
        self.speed_label = tk.Label(self.frame, text="Speed: \n0.00 CPS\n0.00 CPM\n0.00 WPS\n0.00 WPS", font=("Helventica", 18))
        self.speed_label.grid(row=2, column=0, columnspan=2, padx=5, pady=10)

        # creating a reset button
        self.reset_button = tk.Button(self.frame, text="Reset", command=self.reset, font=("Helventica", 24))
        self.reset_button.grid(row=3, column=0, columnspan=2, padx=5, pady=10)

        self.frame.pack(expand=True)


        # adding the boolean to know that the app is started or not
        self.counter = 0
        self.running = False

        self.root.mainloop()

Step 5: Define the start function

We already defined in the above section that the timer will start when the user presses the key but we don’t want to start it if we pressed the shift key or ctrl key or any other modifier key. So we are adding that function using the if statement and we also need to set the if statement for typing wrong input because if the user can press the key then this doesn’t make sense. 

Define the color of the text:

  1. If the user will type the wrong word then the color of the text will convert into red color
  2. If the user will type the right word then the color of the text will remain black
  3. And if the user completed the sentence without any error then the color of the text will change to green color.
def start(self ,event):
        if not self.running:
            if not event.keycode in [16, 17, 18]:
                self.running = True
                t = threading.Thread(target=self.time_thread)
                t.start()
        if not self.sample_label.cget('text').startswith(self.input_entry.get()):
            self.input_entry.config(fg="red")
        else:
            self.input_entry.config(fg="black")
        if self.input_entry.get() == self.sample_label.cget('text')[:-1]:
            self.running = False
            self.input_entry.config(fg="green")

Step 6: Define the time function in a different thread

This section of the code is pretty simple here we created a function to display time in CPS (Character per second), CPM (Character per minute), WPS (Word per second), and WPM (Word per minute).

def time_thread(self):
        while self.running:
            time.sleep(0.1)
            self.counter += 0.1
            cps = len(self.input_entry.get()) / self.counter
            cpm = cps * 60
            wps = len(self.input_entry.get().split(" ")) / self.counter
            wpm = wps * 60
            self.speed_label.config(text=f"Speed: \n{cps:.2f} CPS\n{cpm:.2f} CPM\n{wps:.2f} WPS\n{wpm:.2f} WPM")

Step 7: Define the reset function

def reset(self):
        self.running = False
        self.counter = 0
        self.speed_label.config(text="Speed: \n0.00 CPS\n0.00 CPM\n0.00 WPS\n0.00 WPM")
        self.sample_label.config(text=random.choice(self.text))
        self.input_entry.delete(0, tk.END)

Output:

As you can see in the output that program is working just fine.

Final Words

Well, this is how you can test your typing speed using Python. This typing speed tester is an example of how you can use the power of programming to create something that is fun and interactive. We created this using python which is implemented using the python Tkinter library and can be customized easily. We hope you enjoyed this quick and easy typing speed test! If you have any questions about this typing speed test or about programming in general, please contact us.

Here are some useful tutorials that you can read: