# Let’s Create Typing Speed Calculator Using Python (GUI)

In this blog, we will learn how to create a game of typing speed per minute.

Let us first understand what Calculate Typing Speed Per Second is?
This type of game includes some time and during this time duration, we type the words and check our typing speed.

Now the question arises, how to check our Typing Speed?
The number of words someone can accurately type in one minute.

We created a list of random words in the program, random words will appear on the screen and the user has to complete the task within a minute.

The formula for calculating Typing Speed Per Second:
Formula:-
Speed Per Second = (sum of all typed character / 60)

Now let us start a project using Python GUI for Typing Speed Per Second is :

Code for the project:

For the code of this project click on the link below:

https://github.com/Platforuma/medium_blog/blob/178ccb2a29402f2f1840b2e367c57d8504d648c6/newtyping.py

Define a time function. In the previous code of the project, we already comprehend what the function is.
Define global variable time left, score, miss, speed and total. In the previous code of the project, we already comprehend what the variable and global variable is.

Define the if condition and pass, when the condition is true.
When if the condition is not true then the program switches to the else condition and applies the configure method with FG.
Again define the if condition, where we provide some condition on the time, left variable.

When the condition goes true then decrease the timeleft value.
Using the configure we show the timeLabelCount value on the GUI window.
Here in this code, we interpret after function, after the function is also a Universal function that can be used directly on the root as well as with other widgets.
In this code, we interpret the if condition again where timeleft==0 and pass the program on the next line of the code when the condition is true.

if(timeleft==0):
speed = speed.join(letter)
total = ((len(speed)) / 60)
print(‘Typing Speed is ‘, math.ceil(total),’letter/sec’)

Again we define the if condition, given condition timeleft==0, if the condition goes true then the program goes to the next statement of the Project.
Here we explain the speed variable where we apply to join the letter and store it in the speed variable.
Again we define the total variable to store the value of typing character / 60. Here typing character / 60 is the formula for calculating the typing speed in this project.
Now in the print statement, we print the Typing Speed value where we used the math module function in a calculation.

else:
gamePlayDetailLabel.configure(text=’Hit = {} | Miss = {} | Total Score = {}’.format(score,miss,score-miss))
speedperseclLabel.configure(text=’Typing Speed in letter/sec = {}’.format(math.ceil(total)))
if(rr==True):
score = 0
timeleft = 60
miss = 0
timeLabelCount.configure(text=timeleft)
wordLabel.configure(text=words)
scoreLabelCount.configure(text=score)

When if the condition goes false we define else condition.
Again we used the Configure method for displaying the Label content on the GUI window.

In the next line of code, we define the rr variable to show a message box and ask the user to retry and cancel the game. When the timer is completed one pop notification will generate, notification work is to ask the user to continue the game or close the game.

(For more information on the message box, kindly refer to this documentation)

Again define the if condition where rr == True.
Define the score variable with 0. In the previous line of code, we already know what variable is?
Define a timeleft variable with 60.
Define miss variable with 0.
Configure Label to show text on GUI window from timeleft.
Configure Label to show text on GUI window from words.
Configure Label to show text on GUI window from the score.

def startGame(event):
global score, miss , speed
if(timeleft == 60):
time()
gamePlayDetailLabel.configure(text=’’)
if(wordEntry.get() == wordLabel[‘text’]):
score += 1
scoreLabelCount.configure(text=score)
if(wordEntry.get() == wordLabel[‘text’]):
letter.append(wordEntry.get())

In this line of code, we define the function start game.

Define global variable score, miss and speed. In the antecedent code of the project, we already know what the function is.
Define the if condition where the condition is given timeleft == 60.
When if the condition gets true define time().
Next time define the configure method on the Label to display text on the GUI window from the text.

In this line of code, we define the if condition when we get the Entry == wordLabel text.
When if the condition goes true then interpret score variable value increases.
Display the score on the GUI window from the text variable.
Next time, define the if condition when we get the Entry == wordLabel text. When the condition gets true, get the value from wordEntry and append value in the letter.

else:
miss += 1
random.shuffle(words)
wordLabel.configure(text=words)
wordEntry.delete(0,END)

In this statement, we define the else condition. When if the condition is false then define miss variable value is an increment.
Now define the random. shuffle function on the elements of the word.

Display text using the Configure Method on the GUI window from the text.
Define word entry.delete(0,END) to delete Tkinter Text Box’s Contents. Here delete method is used to delete the previous value and display the next value.

from Tkinter import *
import random
from Tkinter import message box
import math

Tkinter is the name of the GUI library in Python. Creating a GUI application using Tkinter is an easy task, so we used it in our project Tkinter Module. (Tutorial link for Tkinter)
Import * means everything from the library. Import * is allowing a Python file to access the script from another Python file.

Here we Import a random module for access to the random library of python. When we import the random module in a program then random value provides the access of random value of given range or given data.

Import message box for access message box library means that when we want to display any message in our project so for this message box we import the message box. Again message box is the python library.

(For more information on the message box module, kindly refer to this documentation)

Here we import math for access to the math library. When we do mathematical functions in our project we need to import math modules.

root = Tk()
root.geometry(“800x550+400+100”)
root.configure(bg=”powder blue”)
root.title(“Typing Speed Increaser Game”)

Tk class is used to create a root window. The root is the name of a root window. Here Tk function provides a window of GUI as well as provides so many features like setting the title, set the geometry of the GUI window.

(About the Title and Geometry we learn in the next steps of the program)

Tkinter provides many methods, one of them is the geometry() method. This method is used to set the dimensions of the Tkinter window and it is used to set the position of the main window on the user desktop.

Here configure method allows to give the bg colour of the GUI window.

In these steps, we are giving the title of the GUI root window. The title is defined about the project in one line what we do in the project.

(For more information on Geometry or title, kindly refer to this documentation)

score = 0
timeleft = 60
count = 0
sliderwords = ‘’
miss = 0
letter = []
speed = ‘’
total = ‘’

Here we define a variable score with 0 value, time left with 60 value, count with 0 value, the slider works with a null value, miss with 0 value, a letter with a null list, the speed with a null value and total with a null value.

fontLabel = Label(root, text=” “, font=(“arial”,25, “italic bold”),
bg=”powder blue”,fg=”red”,width=40)
fontLabel.place(x=10, y=10)
labelSlider()

random.shuffle(words)

Now come to Designing part of the GUI project. Here we are using some Label, Entry Box and Button in a project. So let's start to understand the working of Label, EntryBox and Button in a project accordingly to the project code line by line.

Here we define a font label Label on the GUI window and write text on Label and set the font and set FG, bg or width of the label.
Now in this line, we are working on the place method. Now assign the place of the font label Label using x or y coordinates values.

This line defines the labelSlider function.

Define random. shuffle function. In the previous line of code, we understand how random modules work.

wordLabel = Label(root,text=words,font=(“arial”,40, “italic bold”),bg=’powder blue’)
wordLabel.place(x=350, y=200)

scoreLabel = Label(root,text=”Your Score :”,font=(“arial”,25, “italic bold”),bg=”powder blue”)
scoreLabel.place(x=10,y=100)

Again define a word label Label on GUI window and write text on Label and set font and bg of the label.
Now in this line, we are working on the place method. Now assign the place of the word label Label using x or y coordinate values.

In this statement of code, we define a score label Label on the GUI window and write text on Label and set font and bg of the label.
Again assign the place of the scoreLabel Label using x or y coordinate values.

scoreLabelCount = Label(root,text=score,font=(“arial”,25, “italic bold”),bg=”powder blue”,fg=’blue’)
scoreLabelCount.place(x=80,y=180)

timerCount = Label(root,text=”Time Left :”,font=(“arial”,25, “italic bold”),bg=”powder blue”)
timerCount.place(x=600,y=100)

timeLabelCount = Label(root,text=timeleft,font=(“arial”,25, “italic bold”),bg=”powder blue”,fg=’blue’)
timeLabelCount.place(x=680,y=180)

Again define a scoreLabelCount Label on GUI window and write text on Label and set font and bg or FG of the label.
Then assign the place of the scoreLabelCount Label using x or y coordinate values.

The label is nothing but it is used to display content on the GUI window.

Now define a timeout Label on the GUI window and write text on Label and set the font and bg of the label.
Then assign the place of the timer count Label using x or y coordinates values.

Now define a timeLabelCount Label on GUI window and write text on Label and set font and bg of the label.
Assign the place of the timeLabelCount Label using x or y coordinate values.

gamePlayDetailLabel = Label(root,text=”Type Word and Hit Button”,font=(“Arial”,20, “italic bold”),bg=”powder blue”,fg=’violet red’)
gamePlayDetailLabel.place(x=180,y=450)
speedparcelLabel = Label(root,font=(“Arial”,35, “italic bold”),bg=”powder blue”,fg=’red’)
speedperseclLabel.place(x=85,y=480)

Define a gamePlayDetailLabel Label on GUI window and write text on Label and set font and bg or FG of the label.
In this line again assign the place of the gamePlayDetailLabel Label using x or y coordinate values.

Define a speedperseclLabel Label on GUI window and write text on Label and set font and bg or FG of the label.
Assign the place of the speedperseclLabel Label using x or y coordinate values.

Here we are using so many Labels to display some content on the GUI window.

wordEntry = Entry(root,font=(“arial”,25, “italic bold”),bd=10,justify=”center”)
wordEntry.place(x=250,y=300)
wordEntry.focus_set()

root.bind(‘<Return>’,startGame)
root.mainloop()

Here in this line of code we define a word entry Entry on the GUI window and set the font, bd and justify of Entry. The entry box used to take an entry from the user or also we can set some value on the Entry Box.

Here assign the place of the word entry Entry using x or y coordinate values.
Define focus_set function of word entry Label. Here we used the focus_set function on the particular widget.

Define bind function on the event <Return> to a Button, when the command to be run after hitting Enter. When we click on the enter button then the program gives some execution so the main work of the bind function we bind our code with events.

Now come to the last line of code of the project is the main loop function that provides an infinite loop. So let’s start to understand how it works.
mainloop() is an infinite loop used to run the application, using this function window is not closed as long as.