import tkinter as tk
import random
import time
# 建立句子庫
sentences = [
"The quick brown fox jumps over the lazy dog.",
"Python is a powerful programming language.",
"Practice makes perfect.",
"Typing is a useful skill.",
"Artificial intelligence is changing the world."
]
# 全域變數
target_sentence = random.choice(sentences)
start_time = 0
question_count = 0
total_time = 0
total_accuracy = 0
results = [] # 儲存每題耗時與正確率
def start_typing():
global start_time
entry.delete(0, tk.END)
entry.focus()
start_time = time.time()
result_label.config(text="")
def check_typing(event=None):
global start_time, question_count, total_time, total_accuracy, results
end_time = time.time()
user_input = entry.get()
elapsed_time = round(end_time - start_time, 2)
correct = sum(1 for a, b in zip(target_sentence, user_input) if a == b)
accuracy = round(correct / len(target_sentence) * 100, 2)
# 統計資料儲存
question_count += 1
total_time += elapsed_time
total_accuracy += accuracy
results.append((question_count, elapsed_time, accuracy))
if question_count == 5:
show_summary()
else:
next_question()
def next_question():
global target_sentence
target_sentence = random.choice(sentences) # ✅ 真正換句子
sentence_label.config(text=target_sentence)
entry.delete(0, tk.END)
entry.focus()
def show_summary():
avg_accuracy = round(total_accuracy / 5, 2)
result_text = "📊 練習結束!以下是你的統計資料:\n\n"
for num, t, acc in results:
result_text += f"第 {num} 題:耗時 {t} 秒,正確率 {acc}%\n"
result_text += f"\n✅ 總耗時:{round(total_time, 2)} 秒"
result_text += f"\n✅ 平均正確率:{avg_accuracy}%"
result_label.config(text=result_text)
entry.config(state='disabled') # 鎖住輸入框
sentence_label.config(text="練習結束,請重新啟動程式再來一次")
# 建立主視窗
window = tk.Tk()
window.title("打字練習程式")
window.geometry("800x500")
# 「開始」按鈕放右上
start_button = tk.Button(window, text="開始", command=start_typing)
start_button.place(x=730, y=10)
# 題目區
tk.Label(window, text="請輸入以下句子:", font=("Arial", 16)).pack(pady=20)
sentence_label = tk.Label(window, text=target_sentence, font=("Arial", 12), fg="blue", wraplength=750)
sentence_label.pack()
# 輸入框
entry = tk.Entry(window, font=("Arial", 12), width=80)
entry.place(x=20, y=320)
entry.bind("<Return>", check_typing)
# 顯示結果與統計
result_label = tk.Label(window, text="", font=("Arial", 14), justify="left")
result_label.pack(pady=10)
# 啟動主程式
window.mainloop()
沒有留言:
張貼留言