本次程序中,我們使用的python庫完全是python的內(nèi)置庫,其中界面的制作是利用tkinter進(jìn)行制作。核心程序可以分為三個(gè)部分,分別為:
而對(duì)比文本的顯示,則是通過調(diào)用self.getSentence函數(shù)來實(shí)現(xiàn),程序讀取本地的sentences.txt文本后,讀取所有的文本內(nèi)容,其中每一行都是一個(gè)獨(dú)立的句子。
在界面中顯示文本后,接下來就是在下方的輸入框中,抄寫上面的文本內(nèi)容。這里的文本內(nèi)容,我們是通過tkinter庫中的StringVar對(duì)象來進(jìn)行跟蹤,程序如下圖所示:
當(dāng)我們輸入文本時(shí),通過StringVar對(duì)象的trace函數(shù)來實(shí)時(shí)跟蹤文本,并執(zhí)行self.check函數(shù),self.check函數(shù)的作用是當(dāng)開始輸入文本時(shí),設(shè)置self.start_time為文本輸入的時(shí)間。
程序獲取用戶輸入的文本內(nèi)容,然后通過計(jì)算用戶的輸入文本和正確的文本之間的匹配程序來計(jì)算打字的準(zhǔn)確率,通過計(jì)算用戶打字的計(jì)算時(shí)間來計(jì)算用戶的打字速度,并顯示在界面中,效果如下圖所示:
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="結(jié)果", 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="準(zhǔn)確率: 00%", font=(
"宋體", 15, "bold"), bg="#f28500", fg="#ffffff")
self.accu_label.place(x=380, y=380)
self.time_label = Label(self.root, text="時(shí)間: 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="準(zhǔn)確率: 00%")
self.time_label.config(text="時(shí)間: 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="準(zhǔn)確率: {}%".format(accu))
self.time_label.config(text="時(shí)間: {} 秒".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__':
到此這篇關(guān)于用Python實(shí)現(xiàn)一個(gè)打字測試工具來測試你的打字速度的文章就介紹到這了,更多相關(guān)Python打字測試器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!