【Python实践】五笔输入法练习软件

本文详细描述了使用Python的Tkinter库开发的一款五笔输入法练习软件,包含随机选择汉字、输入验证、统计正确与错误次数等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

帮别人写了一个五笔输入法练习软件,记录一下

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()

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值