帮别人写了一个五笔输入法练习软件,记录一下
import tkinter as tk
from tkinter import messagebox
import random
# 扩展的汉字与五笔编码对应关系
word_to_wubi = {"汉": "iygg", "字": "pgh", "软": "bcu", "件": "lwl"}
# 随机选择一个汉字
def choose_random_word():
return random.choice(list(word_to_wubi.keys()))
# 更新界面显示的汉字及其五笔编码
def update_display():
new_word = choose_random_word()
current_word.set(new_word)
# 校验输入的五笔编码是否正确,并进行统计
def check_input(event=None):
input_code = input_var.get().strip()
correct_code = word_to_wubi[current_word.get()]
if input_code == correct_code:
correct_count.set(correct_count.get() + 1)
messagebox.showinfo("正确", "输入正确!")
update_display() # 输入正确后自动切换到下一个汉字
else:
wrong_count.set(wrong_count.get() + 1)
messagebox.showerror("错误", f"输入错误,请重新输入!正确的五笔编码为:{correct_code}")
input_var.set("") # 清空输入框
# 阻止使用退格键
def disable_backspace(event):
if event.keysym == 'BackSpace':
return "break" # 阻止事件继续传播
if __name__ == '__main__':
root = tk.Tk()
root.title("五笔练习软件")
current_word = tk.StringVar(value=choose_random_word())
input_var = tk.StringVar()
correct_count = tk.IntVar()
wrong_count = tk.IntVar()
tk.Label(root, textvariable=current_word, font=('Arial', 20)).pack()
tk.Label(root, text="请输入上方汉字的五笔编码:", font=('Arial', 14)).pack()
entry = tk.Entry(root, textvariable=input_var, font=('Arial', 20))
entry.pack()
entry.bind("<Return>", check_input)
entry.bind("<KeyPress>", disable_backspace) # 绑定按键事件,用于阻止退格键
tk.Button(root, text="切换汉字", command=update_display).pack(pady=10)
tk.Label(root, text="正确次数:", font=('Arial', 14)).pack(side=tk.LEFT)
tk.Label(root, textvariable=correct_count, font=('Arial', 14)).pack(side=tk.LEFT)
tk.Label(root, text="错误次数:", font=('Arial', 14)).pack(side=tk.LEFT)
tk.Label(root, textvariable=wrong_count, font=('Arial', 14)).pack(side=tk.LEFT)
root.mainloop()