import tkinter as tk
from tkinter.messagebox import *
root = tk.Tk()
v1 = tk.StringVar()
v2 = tk.StringVar()
expected_text = """
The Zen of Python,by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one--and preferably only one--obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain,it's a bad idea.
If the implementation is easy to explain,it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
tk.Message(root, text=expected_text,justify = tk.LEFT).pack()
def calculate_accuracy(event):
typed_text = text.get("1.0", "end-1c")
correct_chars = sum(typed_char == expected_char for typed_char, expected_char in zip(typed_text, expected_text))
accuracy = correct_chars / len(expected_text) * 100
progress = len(typed_text) / len(expected_text) * 100
v1.set(f"准确率:{accuracy:.2f}%")
v2.set(f"完成进度:{progress:.2f}%")
# 清除之前的样式
text.tag_remove("error", "1.0", "end")
# 标记错误字符为红色
for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):
if typed_char != expected_char:
text.tag_add("error", f"1.{i-1}", f"1.{i}")
text.tag_config("error", foreground="red")
if progress >= 100:
showinfo("结果", f"完成了,准确率:{accuracy:.2f}%")
root.quit()
text = tk.Text(root, width=80, height=40)
text.pack()
accuracy_label = tk.Label(root, textvariable=v1)
accuracy_label.pack()
progress_label = tk.Label(root, textvariable=v2)
progress_label.pack()
text.bind("<KeyRelease>", calculate_accuracy)
root.mainloop()
这是成品
欢迎改进
欢迎吐槽
欢迎观看 |