AI生成英语辅助背单词python程序

import pandas as pd
import random
import json
import os
import tkinter as tk
from tkinter import filedialog, ttk
import pygame  # 导入pygame库

# 全局变量
data = []
column_names = []
correct_words = set()
current_word = {}
audio_path = ""  # 音频路径
progress_file = 'progress.json'  # 缓存文件名

# 初始化pygame的音频模块
pygame.mixer.init()


# GUI 选择文件和列
def select_file():
    file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xls *.xlsx")])
    if file_path:
        excel_file_label.config(text=f"已选择文件: {file_path}")
        load_excel_data(file_path)


def load_excel_data(file_path):
    global data, column_names
    try:
        df = pd.read_excel(file_path)
        column_names = df.columns.tolist()
        data = df.to_dict(orient='records')
        setup_column_selection()
    except Exception as e:
        feedback_label.config(text=f"无法加载 Excel 文件: {e}")


# 设置选择提问列和回答列
def setup_column_selection():
    question_column_menu['values'] = column_names
    answer_column_menu['values'] = column_names


# 选择音频文件夹
def select_audio_folder():
    global audio_path
    audio_path = filedialog.askdirectory()
    if audio_path:
        audio_folder_label.config(text=f"已选择音频文件夹: {audio_path}")
    else:
        audio_folder_label.config(text="未选择音频文件夹")


# 播放音频
def play_audio(word):
    if play_audio_enabled.get():  # 如果音频播放开关是开启的
        if audio_path:
            audio_file = os.path.join(audio_path, f"{word}.wav")
            if os.path.exists(audio_file):
                try:
                    pygame.mixer.music.load(audio_file)
                    pygame.mixer.music.play()
                except Exception as e:
                    feedback_label.config(text=f"无法播放音频: {e}", fg="red")
            else:
                feedback_label.config(text="音频文件不存在", fg="red")
        else:
            feedback_label.config(text="请先选择音频文件夹", fg="red")


# 保存进度
def save_progress():
    with open(progress_file, 'w') as f:
        json.dump({'correct_words': list(correct_words)}, f)


# 恢复进度
def load_progress():
    if os.path.exists(progress_file):
        with open(progress_file, 'r') as f:
            progress = json.load(f)
            return set(progress.get('correct_words', []))
    return set()


# 开始测试
def start_test():
    global correct_words
    correct_words = load_progress()  # 恢复进度
    test_vocabulary()


# 随机选择一个单词
def test_vocabulary():
    global current_word
    selected_question_col = question_column.get()
    selected_answer_col = answer_column.get()

    remaining_words = [item for item in data if item[selected_answer_col] not in correct_words]
    if remaining_words:
        current_word = random.choice(remaining_words)
        chinese = current_word[selected_question_col]
        word_label.config(text=f"中文翻译: {chinese}")
        input_entry.delete(0, tk.END)
        feedback_label.config(text="")  # 清除之前的反馈

        # 播放音频
        play_audio(current_word[selected_answer_col])  # 播放与单词匹配的音频

    else:
        word_label.config(text="恭喜你,所有单词都记住了!")
        feedback_label.config(text="")
        # 删除进度文件,先检查文件是否存在
        if os.path.exists(progress_file):
            os.remove(progress_file)


# 检查用户输入
def check_answer(event=None):  # 绑定事件按键
    global current_word
    selected_answer_col = answer_column.get()
    english = current_word[selected_answer_col]

    user_input = input_entry.get().strip()

    if user_input.lower() == english.lower():
        feedback_label.config(text="正确!", fg="green")
        correct_words.add(english)
        save_progress()
    else:
        feedback_label.config(text=f"错误!正确的英文单词是: {english}", fg="red")

    root.after(1000, test_vocabulary)  # 延迟1秒后自动进行下一个测试


# GUI 界面设置
root = tk.Tk()
root.title("单词记忆测试")
root.geometry("800x600")

# 音频播放开关需要在root初始化之后
play_audio_enabled = tk.BooleanVar(value=True)  # 音频播放开关,默认开启

# 文件选择按钮
tk.Button(root, text="选择Excel文件", command=select_file).pack()

# 显示已选择的Excel文件
excel_file_label = tk.Label(root, text="未选择文件")
excel_file_label.pack()

# 提问列选择
question_column_label = tk.Label(root, text="选择提问的列(中文)")
question_column_label.pack()
question_column = tk.StringVar()
question_column_menu = ttk.Combobox(root, textvariable=question_column)
question_column_menu.pack()

# 回答列选择
answer_column_label = tk.Label(root, text="选择回答的列(英文)")
answer_column_label.pack()
answer_column = tk.StringVar()
answer_column_menu = ttk.Combobox(root, textvariable=answer_column)
answer_column_menu.pack()

# 音频文件夹选择按钮
tk.Button(root, text="选择音频文件夹", command=select_audio_folder).pack()

# 显示已选择的音频文件夹
audio_folder_label = tk.Label(root, text="未选择音频文件夹")
audio_folder_label.pack()

# 添加音频播放开关
audio_toggle = tk.Checkbutton(root, text="播放音频", variable=play_audio_enabled)
audio_toggle.pack()

# 单词展示标签
word_label = tk.Label(root, text="单词会显示在这里")
word_label.pack()

# 用户输入框
input_entry = tk.Entry(root)
input_entry.pack()
input_entry.bind('<Return>', check_answer)  # 按回车键时检查答案

# 反馈标签
feedback_label = tk.Label(root, text="")
feedback_label.pack()


# 清除缓存并重新测试
def clear_cache_and_restart():
    global correct_words
    if os.path.exists(progress_file):
        os.remove(progress_file)
    correct_words = set()
    feedback_label.config(text="缓存已清除!")
    start_test()  # 重新开始测试


# 开始测试按钮
tk.Button(root, text="开始测试", command=start_test).pack()

# 添加清除缓存并重新测试按钮
tk.Button(root, text="清除缓存并重新测试", command=clear_cache_and_restart).pack()

root.mainloop()

ChatGPT生成,可打包成windows可执行程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Anxonz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值