Typing Speed Test Application

from tkinter import *

import ctypes

import random

import tkinter

ctypes.windll.shcore.SetProcessDpiAwareness(1)

storage = Tk()

storage.title('Typing Speed Test')

storage.geometry('1400x700')

storage.option_add("*Label.Font", "consolas 30")

storage.option_add("*Button.Font", "consolas 30")

def handlingLabels():

    random_selection = [

        'Software engineering is the branch of computer science that deals with the design, development, testing, and maintenance of software applications. Software engineers apply engineering principles and knowledge of programming languages to build software solutions for end users.',

        'A web developer is a programmer who develops World Wide Web applications using a client–server model. The applications typically use HTML, CSS, and JavaScript in the client, and any general-purpose programming language in the server. HTTP is used for communications between client and server.'

    ]

    text = random.choice(random_selection).lower()

    splitPoint = 0

    global nameLabelLeft

    nameLabelLeft = Label(storage, text=text[0:splitPoint], fg='green')

    nameLabelLeft.place(relx=0.5, rely=0.5, anchor=E)

    global nameLabelRight

    nameLabelRight = Label(storage, text=text[splitPoint:])

    nameLabelRight.place(relx=0.5, rely=0.5, anchor=W)

    global currentAlphabetLabel

    currentAlphabetLabel = Label(storage, text=text[splitPoint], fg='grey')

    currentAlphabetLabel.place(relx=0.5, rely=0.6, anchor=N)

    global secondsLeft

    headingLabel = Label(storage, text=f'CopyAssignment - Typing Speed Test', fg='blue')

    headingLabel.place(relx=0.5, rely=0.2, anchor=S)

    secondsLeft = Label(storage, text=f'0 Seconds', fg='red')

    secondsLeft.place(relx=0.5, rely=0.4, anchor=S)

    global writeAble

    writeAble = True

    storage.bind('<Key>', handlekeyPress)

    global secondsPassed

    secondsPassed = 0

    storage.after(60000, stopGame)

    storage.after(1000, timeAddition)

def stopGame():

    global writeAble

    writeAble = False

    amountWords = len(nameLabelLeft.cget('text').split(' '))

    secondsLeft.destroy()

    currentAlphabetLabel.destroy()

    nameLabelRight.destroy()

    nameLabelLeft.destroy()

    global labelOfResult

    labelOfResult = Label(storage, text=f'Words per Minute (WPM): {amountWords}', fg='black')

    labelOfResult.place(relx=0.5, rely=0.4, anchor=CENTER)

    global showcaseResults

    showcaseResults = Button(storage, text=f'Retry', command=restartGame)

    showcaseResults.place(relx=0.5, rely=0.6, anchor=CENTER)

def restartGame():

    labelOfResult.destroy()

    showcaseResults.destroy()

    handlingLabels()

def timeAddition():

    global secondsPassed

    secondsPassed += 1

    secondsLeft.configure(text=f'{secondsPassed} Seconds')

    if writeAble:

        storage.after(1000, timeAddition)

def handlekeyPress(event=None):

    try:

        if event.char.lower() == nameLabelRight.cget('text')[0].lower():

            nameLabelRight.configure(text=nameLabelRight.cget('text')[1:])

            nameLabelLeft.configure(text=nameLabelLeft.cget('text') + event.char.lower())

            currentAlphabetLabel.configure(text=nameLabelRight.cget('text')[0])

    except tkinter.TclError:

        pass

handlingLabels()

storage.mainloop()


No comments: