📜  如何使用Python测试打字速度?

📅  最后修改于: 2022-05-13 01:55:21.687000             🧑  作者: Mango

如何使用Python测试打字速度?

先决条件: Python GUI – tkinter

在本文中,我们将使用Python语言创建一个程序,通过一个基本的 GUI 应用程序测试用户的打字速度。在这里,像TkinterTimeit这样的Python库分别用于 GUI 和计算时间以进行速度测试。此外,随机函数用于获取随机词以进行速度测试计算。以下命令用于安装上述库:

pip install tkintertable
pip install pytest-timeit

首先,导入所有如上所述安装的库,并使用自下而上的方法创建使用Python测试打字速度的程序。

下面是实现。

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 label method of tkinter for labeling in window
    x2 = Label(windows, text=words[word], font="times 20")
 
    # place of labeling 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()


输出:

在上面的代码中,我们首先使用 Tkinter 创建了速度测试窗口。该函数被定义用于在用户输入后计算和打印正确的输出。为用户提供了一个特定的单词列表,用于输入和测试输入速度。为此,我们提供了一个单词列表并使用 random函数生成它们。