而对比文本的显示,则是通过调用self.getSentence函数来实现,程序读取本地的sentences.txt文本后,读取所有的文本内容,其中每一行都是一个独立的句子。
在界面中显示文本后,接下来就是在下方的输入框中,抄写上面的文本内容。这里的文本内容,我们是通过tkinter库中的StringVar对象来进行跟踪,程序如下图所示:
当我们输入文本时,通过StringVar对象的trace函数来实时跟踪文本,并执行self.check函数,self.check函数的作用是当开始输入文本时,设置self.start_time为文本输入的时间。
程序获取用户输入的文本内容,然后通过计算用户的输入文本和正确的文本之间的匹配程序来计算打字的准确率,通过计算用户打字的计算时间来计算用户的打字速度,并显示在界面中,效果如下图所示:
import time
from random import choice
from tkinter import Tk, Label, CENTER, LEFT, StringVar, Entry,Button,DISABLED, END,NORMAL
class typeSpeed(object):
def __init__(self):
self.start_time = 0
self.sentence_words_num = 0
self.sentence = ""
self.root = Tk()
self.root.geometry("900x450+300+100")
self.root.title("Python打字测速")
self.root.config(bg="#FFFF00")
Label(self.root, text="打字速度测试器", anchor=CENTER, font=(
"times new roman", 50, "bold"), bg="#00154D", fg="#F2BC90").place(x=200, y=30)
self.sentence_label = Label(self.root, text="欢迎使用打字速度测试器",
wraplength=400, anchor=CENTER, font=("宋体", 15, "bold"), bg="#00154D", fg="#ffffff", width=40, justify=LEFT)
self.sentence_label.place(x=200, y=150)
self.text = StringVar()
self.text.trace("w", lambda name, index, mode, text=self.text: self.check(text))
self.input_entry = Entry(self.root, font=("宋体", 15, "bold"),
width=40, textvariable=self.text)
self.input_entry.place(x=200, y=250)
reset_button = Button(self.root, text="重置", font=(
"宋体", 18, "bold"), width=12, bg="#808080", command=self.Reset)
reset_button.place(x=120, y=320)
changetext_button = Button(self.root, text="切换文本", font=(
"宋体", 18, "bold"), width=12, bg="#808080", command=self.getSentence)
changetext_button.place(x=360, y=320)
result_button = Button(self.root, text="结果", font=(
"宋体", 18, "bold"), width=12, bg="#808080", command=self.result)
result_button.place(x=600, y=320)
self.speed_label = Label(self.root, text="速度: 00 字每分钟", font=(
"宋体", 15, "bold"), bg="#f28500", fg="#ffffff")
self.speed_label.place(x=120, y=380)
self.accu_label = Label(self.root, text="准确率: 00%", font=(
"宋体", 15, "bold"), bg="#f28500", fg="#ffffff")
self.accu_label.place(x=380, y=380)
self.time_label = Label(self.root, text="时间: 0 秒", font=(
"宋体", 15, "bold"), bg="#f28500", fg="#ffffff")
self.time_label.place(x=620, y=380)
self.getSentence()
self.root.mainloop()
def Reset(self):
self.input_entry.config(state=NORMAL)
self.input_entry.delete(0, END)
self.start_time = 0
self.speed_label.config(text="速度: 00字每分钟")
self.accu_label.config(text="准确率: 00%")
self.time_label.config(text="时间: 0 秒")
def getSentence(self):
self.Reset()
with open("./sentences.txt", "r", encoding="utf-8") as f:
sentences = f.readlines()
self.sentence = choice(sentences).rstrip()
self.sentence_label.config(text=self.sentence)
self.sentence_words_num = len(self.sentence)
def result(self):
duration = round(time.time() - self.start_time)
input_text = self.text.get()
wpm = round((len(input_text)/duration)*60)
count = 0
for index, char in enumerate(input_text):
if self.sentence[index] == char:
count += 1
accu = round((count/self.sentence_words_num)*100)
self.speed_label.config(text="速度: {} 字每分钟".format(wpm))
self.accu_label.config(text="准确率: {}%".format(accu))
self.time_label.config(text="时间: {} 秒".format(duration))
def check(self, text):
if self.start_time == 0 and len(text.get()) == 1:
self.start_time = time.time()
elif len(text.get()) == self.sentence_words_num:
self.input_entry.config(state=DISABLED)
self.result()
if __name__ == '__main__':
到此这篇关于用Python实现一个打字测试工具来测试你的打字速度的文章就介绍到这了,更多相关Python打字测试器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!