import tkinter as tk import csv import random from struct import pack # 从CSV读取数据 def read_words_from_csv(file_path): with open(file_path, 'r', encoding='utf-8') as csvfile: reader = csv.reader(csvfile) next(reader) # 过表头 return list(reader) # 随机选取固定数量的单词 def select_random_words(words, count=5): return random.sample(words, count) # 创建并显示单词及按钮 def display_words(window, words, word_index, total_words): if word_index < total_words: number, word = word_index + 1, words[word_index][1] tk.Label(window, text=f"{number}: {word}", font=("Arial", 12)).pack(pady=5) tk.Button(window, text="认识", command=lambda: next_word(window, word_index +1)).pack(side=tk.LEFT, padx=10, pady=5) tk.Button(window, text="不认识", command=lambda: next_word(window, word_index)).pack(side=tk.RIGHT, padx=1, pady=5) else: tk.Label(window, text="所有单词已显示完毕", font=("Arial", 14, 'bold')).pack() def next_word(window, word_index, total_words): window.destroy() new_window = tk.Toplevel() new_window.title("单词学习") display_words(new_window, word_index, total_words) # 主程序 def main(): # 读取单词 words = read_words_from_csv('words.csv') window = tk.Tk() window.title("单词学习器") tk.Button(window, text="开始背单词", command=lambda: display_words(tk.Toplevel(), 0, len(words))), pack(pady=10) window.mainloop() if __name__=='__main__': main()