Skip to content
Related Articles

Related Articles

Improve Article
How to test Typing Speed using Python?
  • Difficulty Level : Basic
  • Last Updated : 05 Sep, 2020

Prerequisites: Python GUI – tkinter

In this article, we will create a program test the typing speed of the user with a basic GUI application using Python language. Here the Python libraries like Tkinter and Timeit are used for the GUI and Calculation of time for speed testing respectively.  Also, the Random function is used to fetch the random words for the speed testing calculation. Following command is used to install the above-mentioned libraries:

pip install tkintertable
pip install pytest-timeit

Firstly, all the libraries are imported that are installed as mentioned above, and using the bottom-up approach the programming for testing the typing speed using python is created. 

Below is the implementation.

Python3






# importing all libraries
from tkinter import *
from timeit import default_timer as timer
import random
  
# creating window using gui
window = Tk()
  
# the size of the window is defined
window.geometry("450x200")
  
x = 0
  
# defining the function for the test
def game():
    global x
  
    # loop for destroying the window
    # after on test
    if x == 0:
        window.destroy()
        x = x+1
  
    # defining function for results of test
    def check_result():
        if entry.get() == words[word]:
  
            # here start time is when the window
            # is opened and end time is when
            # window is destroyed
            end = timer()
  
            # we deduct the start time from end
            # time and calculate results using
            # timeit function
            print(end-start)
        else:
            print("Wrong Input")
  
    words = ['programming', 'coding', 'algorithm',
             'systems', 'python', 'software']
  
    # Give random words for testing the speed of user
    word = random.randint(0, (len(words)-1))
  
    # start timer using timeit function
    start = timer()
    windows = Tk()
    windows.geometry("450x200")
  
    # use lable method of tkinter for labling in window
    x2 = Label(windows, text=words[word], font="times 20")
  
    # place of labling in window
    x2.place(x=150, y=10)
    x3 = Label(windows, text="Start Typing", font="times 20")
    x3.place(x=10, y=50)
  
    entry = Entry(windows)
    entry.place(x=280, y=55)
  
    # buttons to submit output and check results
    b2 = Button(windows, text="Done",
                command=check_result, width=12, bg='grey')
    b2.place(x=150, y=100)
  
    b3 = Button(windows, text="Try Again"
                command=game, width=12, bg='grey')
    b3.place(x=250, y=100)
    windows.mainloop()
  
  
x1 = Label(window, text="Lets start playing..", font="times 20")
x1.place(x=10, y=50)
  
b1 = Button(window, text="Go", command=game, width=12, bg='grey')
b1.place(x=150, y=100)
  
# calling window
window.mainloop()

Output:

In the above code, we first create the speed testing window using Tkinter. The function is defined for calculating and printing the correct output after the user input. A specific list of words is provided to the user to type and test the speed of typing. For that, we provide a list of words and generate them with the random function. 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :