|
| 1 | +# importing all libraries |
| 2 | +from tkinter import * |
| 3 | +from timeit import default_timer as timer |
| 4 | +import random |
| 5 | + |
| 6 | +# creating window using gui |
| 7 | +window = Tk() |
| 8 | + |
| 9 | +# the size of the window is defined |
| 10 | +window.geometry("450x200") |
| 11 | + |
| 12 | +x = 0 |
| 13 | + |
| 14 | +# defining the function for the test |
| 15 | +def game(): |
| 16 | + global x |
| 17 | + |
| 18 | + # loop for destroying the window |
| 19 | + # after on test |
| 20 | + if x == 0: |
| 21 | + window.destroy() |
| 22 | + x = x + 1 |
| 23 | + |
| 24 | + # defining function for results of test |
| 25 | + def check_result(): |
| 26 | + if entry.get() == words[word]: |
| 27 | + |
| 28 | + # here start time is when the window |
| 29 | + # is opened and end time is when |
| 30 | + # window is destroyed |
| 31 | + end = timer() |
| 32 | + |
| 33 | + # we deduct the start time from end |
| 34 | + # time and calculate results using |
| 35 | + # timeit function |
| 36 | + print(end - start) |
| 37 | + else: |
| 38 | + print("Wrong Input") |
| 39 | + |
| 40 | + words = ["programming", "coding", "algorithm", "systems", "python", "software"] |
| 41 | + |
| 42 | + # Give random words for testing the speed of user |
| 43 | + word = random.randint(0, (len(words) - 1)) |
| 44 | + |
| 45 | + # start timer using timeit function |
| 46 | + start = timer() |
| 47 | + windows = Tk() |
| 48 | + windows.geometry("450x200") |
| 49 | + |
| 50 | + # use label method of tkinter for labeling in window |
| 51 | + x2 = Label(windows, text=words[word], font="times 20") |
| 52 | + |
| 53 | + # place of labeling in window |
| 54 | + x2.place(x=150, y=10) |
| 55 | + x3 = Label(windows, text="Start Typing", font="times 20") |
| 56 | + x3.place(x=10, y=50) |
| 57 | + |
| 58 | + entry = Entry(windows) |
| 59 | + entry.place(x=280, y=55) |
| 60 | + |
| 61 | + # buttons to submit output and check results |
| 62 | + b2 = Button(windows, text="Done", command=check_result, width=12, bg="grey") |
| 63 | + b2.place(x=150, y=100) |
| 64 | + |
| 65 | + b3 = Button(windows, text="Try Again", command=game, width=12, bg="grey") |
| 66 | + b3.place(x=250, y=100) |
| 67 | + windows.mainloop() |
| 68 | + |
| 69 | + |
| 70 | +x1 = Label(window, text="Lets start playing..", font="times 20") |
| 71 | +x1.place(x=10, y=50) |
| 72 | + |
| 73 | +b1 = Button(window, text="Go", command=game, width=12, bg="grey") |
| 74 | +b1.place(x=150, y=100) |
| 75 | + |
| 76 | +# calling window |
| 77 | +window.mainloop() |
0 commit comments