Python Typing Test Project

Have you ever taken the typing test to check your typing speed and accuracy? This test not only lets you test yourself but also improves your typing skill in regular practice. Wouldn’t it be amusing to build your own typing tester? In this project, we will build a typing test project in Python. Let’s start!!!

About Typing Test Project

This is a project that allows the user to practice typing. In this project, the user will be given random English words. The user has to type the english words correctly, also considering the letter case.

The time taken by the user to complete typing the words and the number of correctly typed words will be taken as a measure of accuracy.

Python Typing Test Project

In this typing test project, the user will be first shown the main window with the start button. On clicking the start button, another window opens. We will be using the Tkinter module for GUI building.

Here, there will be 10 words to be typed by the user, that are displayed one after another. We will be using the english_words module to get the words. And the amount of time elapsed will also be shown on the screen. After completion of typing, the results will be shown.

Project Prerequisites

It is advised for the developer to have an idea of Python and the Tkinter module prior to building the project. In addition, you need to install Tkinter and english_words modules. You can use the following commands.

pip install tkinter
pip install english-words

Download the Typing Test Project

Please download the source code for the Python typing test project here: Python Typing Speed Test Project

Project File Structure

The steps to build the python typing speed test project are

  1. Importing the modules
  2. Creating the main window
  3. Declaring the global variables
  4. Creating the game window
  5. Writing a function to control time and end of the game
  6. Writing a function that controls the game
  7. Adding the widgets to the game window

1. Importing the modules

# Importing the modules
from english_words import english_words_set
from tkinter import *
import tkinter.font as font
import random

Code explanation:

a. english_words: This module contains the set of English words
b. Tkinter: This module helps in building the screen, add widgets and their attributes
c. tkinter.font: This adds font style to the text
d. random: This module is used here to shuffle the list containing the English words in a random manner

2. Creating the main window

#Creating the main window and adding size, title and color
wn= Tk()
wn.geometry('600x600')
wn.title("PythonGeeks Typing Test")
wn.config(bg='LightBlue1')

#Creating a frame to show the title of the project
headingFrame1 = Frame(wn,bg="snow3",bd=5)
headingFrame1.place(relx=0.2,rely=0.2,relwidth=0.6,relheight=0.16)

headingLabel = Label(headingFrame1, text="Welcome to \n PythonGeeks Typing Test", bg='azure2',fg='black', font=('Courier',15,'bold'))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

#Creating a button to start the game. The startGame function, given as command parameter, runs on clicking the button
btn = Button(wn,text="Start",bg='old lace', fg='black',width=20,height=2,command=startGame)
btn['font'] = font.Font( size=12)
btn.place(x=200,y=300)

wn.mainloop()#Runs the window till it is closed

Code explanation:

a. geometry(): It sets the length and width of the window.
b. configure(): It sets the background color of the screen
c. title(): It is used for the title on the top of the window.
d. Frame(): This creates a widget that holds various widgets on the screen
e. place(): This places the widgets in a specified location based on coordinates or relative to the parent component
f. Label(): This helps in showing text

3. Declaring the global variables

score=0
missed=0
time=0
count=0
words=list(english_words_set)

Code explanation:

a. score: It stores the number of correctly types words
b. missed: It stores the number of wrongly types words
c. time: It counts the seconds
d. count: It counts the number of words displayed
e. words: It stores the english words from the module as list

4. Creating the game window

#Creating the game window and adding size, title and color
wn= Tk()
wn.geometry('700x600')
wn.title('Typing Test By PythonGeeks')
wn.config(bg='honeydew2')

Code explanation:

a. geometry(): It sets the length and width of the window.
b. configure(): It sets the background color of the screen
c. title(): It is used for the title on the top of the window.

5. Writing a function to control time and end of game

This function updates the time after every second. Once all the 10 words are done, it ends the game and shows the result

def timeFunc():
 global time,score,missed,count
 if(count<=10): # If count is less than 10, update the time
  time +=1
  timer.configure(text=time)
  timer.after(1000,timeFunc)
 else: #If count is equal to 10 then show the results, initialize the variables and destroy the other widgets
  #Label to show result after the game ends
  result= Label(wn,text='',font=('arial',25,'italic bold'),fg='grey')
  result.place(x=230,y=250)
  result.configure(text='Time taken = {} \n Score = {} \n Missed = {}'
                     .format(time,score,count-score-1))
  missed=0
  score=0
  time=0
  count=0
  nextWord.destroy()
  userInput.destroy()
  scorelabel.destroy()
  scoreboard.destroy()
  timerlabel.destroy()
  timer.destroy()

Code explanation:

a. configure(): It sets the text in the screen
b. time.after(): After every 1000ms/ 1s it runs the timeFunc() recursively till count of words is less than 10
c. destroy(): This function destroys the widget

6. Writing a function that controls the game

This function runs whenever the user presses the enter button. It
a. calls the timeFunc() function on starting game for updating time continuously.
b. Every other time the user presses the enter button, it checks if the word entered is correct and updates the score accordingly.
c. Then it changes the word to another English word from the list.

def mainGame(event):
 global score, missed,count
 if time==0: #At the starting of the game
  random.shuffle(words) #Shuffle the list words randomly
  nextWord.configure(text=words[0]) #Show the first element of the list in the nextWord label
  userInput.delete(0,END) #clear the entry widget userInput
  timeFunc() #call the time function

 #If the enter button is pressed and it is not the start of the game
 if userInput.get()== nextWord['text']: #check if user entered correctly
  score +=1 #increment score
  scoreboard.configure(text=score) #show the new score on scoreboard
 count+=1 #Increment the count
 if(count<=10): #If count is less the 10
  random.shuffle(words) #Shuffle the list words randomly
  nextWord.configure(text=words[0])#Show the first element of the list in the    nextWord label
  userInput.delete(0,END)#clear the entry widget userInput

Code explanation:

a. random.shuffle(): This function shuffles the elements of the list ‘words’ in a random manner
b. configure(): It sets the text in the screen
c. delete(): This clears the text in the widget
d. get(): This gets the text currently shown by the widget

7. Adding the widgets to the game window

#Creating a label to show the name of the project
label=Label(wn,text='Typing Test By PythonGeeks',font=('arial',25,
           'italic bold'),fg='gray',width=40)
label.place(x=10,y=10)

#label to show the instruction initially and then to show the words to be typed
nextWord=Label(wn,text='Hit enter button to start and after typing the word',font=('arial',20,
            'italic bold'),fg='black')
nextWord.place(x=30,y=240)

#Label to show text ‘Your Score’
scorelabel=Label(wn,text='Your Score:',font=('arial',25,
           'italic bold'),fg='red')
scorelabel.place(x=10,y=100)

#Label to show score
scoreboard=Label(wn,text=score,font=('arial',25,
            'italic bold'),fg='blue')
scoreboard.place(x=100,y=180)
#Label to show text Time Elapsed
timerlabel=Label(wn,text='Time Elapsed:',font=('arial',25,
            'italic bold'),fg='red')
timerlabel.place(x=500,y=100)

#Label to show time
timer=Label(wn,text=time,font=('arial',25,
             'italic bold'),fg='blue')
timer.place(x=560,y=180)

#This widget takes the input from the user
userInput= Entry(wn,font=('arial',25,'italic bold'),bd=10,justify='center')
userInput.place(x=150,y=330)
userInput.focus_set()

wn.bind('<Return>',mainGame)#Runs the mainGame function every time the user presses enter button
wn.mainloop()#Runs the window till it is closed

Code explanation:

a. Frame(): This creates a widget that holds various widgets on the screen
b. place(): This places the widgets in a specified location based on coordinates or relative to the parent component
c. Label(): This helps in showing text
d. Entry(): This takes input from the user
e. Button(): This creates a button with mentioned properties and the command parameter represents the function that is to be executed/operated on clicking the button

Python Typing Test Output

The image of working of the game with words shown and time running

python typing test output

Summary

Hurray! We have successfully built the typing speed test project in Python. We have covered the concepts of various widgets and their properties in the Tkinter module. In addition, we also used the randoms and english_words libraries. Hope you enjoyed developing with us!

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *