1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
| import tkinter as tk from tkinter import messagebox, ttk import json import random import os
class VocabularyApp: def __init__(self, root): self.root = root self.root.title("背单词软件") self.root.geometry("500x800") # 调整窗口大小 # 设置主题颜色 self.bg_color = "#f0f0f0" self.button_color = "#4a90e2" self.button_text_color = "white" self.frame_bg = "#ffffff" self.root.configure(bg=self.bg_color) # 加载单词数据 self.words = self.load_words() self.current_word = None self.practice_mode = False self.exam_mode = False self.exam_words = [] self.current_exam_index = 0 self.correct_count = 0 self.user_answers = [] # 存储用户的答案 self.answer_shown = False # 标记答案是否已显示 self.last_word = None # 存储上一题的单词 # 创建界面元素 self.create_widgets() # 显示第一个单词 self.show_next_word() # 绑定按键 self.root.bind('<Up>', self.handle_show_answer) # 上键显示答案 self.root.bind('<Right>', self.handle_next) # 右键下一题 self.root.bind('<Return>', self.handle_next) # 回车键也支持下一题 def handle_show_answer(self, event): if self.practice_mode and not self.exam_mode: # 只在练习模式下显示答案 self.show_current_answer() def handle_next(self, event): if self.practice_mode or self.exam_mode: self.check_answer() else: self.show_next_word() def show_current_answer(self): if not self.current_word: return if not self.answer_shown: self.word_label.config(text=self.current_word["word"]) self.example_label.config(text=self.current_word["example"]) self.answer_shown = True else: self.word_label.config(text="") self.example_label.config(text="") self.answer_shown = False def load_words(self): # 如果words.json不存在,创建示例数据 if not os.path.exists('words.json'): sample_words = [ {"word": "apple", "meaning": "苹果", "example": "I eat an apple every day."}, {"word": "book", "meaning": "书", "example": "I love reading books."}, {"word": "computer", "meaning": "电脑", "example": "I work on my computer."} ] with open('words.json', 'w', encoding='utf-8') as f: json.dump(sample_words, f, ensure_ascii=False, indent=2) # 加载单词数据 with open('words.json', 'r', encoding='utf-8') as f: return json.load(f) def save_words(self): with open('words.json', 'w', encoding='utf-8') as f: json.dump(self.words, f, ensure_ascii=False, indent=2) def create_widgets(self): # 创建主框架 main_frame = tk.Frame(self.root, bg=self.bg_color) main_frame.pack(expand=True, fill='both', padx=20, pady=20) # 标题 title_label = tk.Label(main_frame, text="单词学习助手", font=("微软雅黑", 24, "bold"), bg=self.bg_color) title_label.pack(pady=(0, 20)) # 搜索框架 search_frame = tk.Frame(main_frame, bg=self.frame_bg, padx=10, pady=10) search_frame.pack(fill='x', pady=(0, 20)) self.search_entry = tk.Entry(search_frame, font=("微软雅黑", 12)) self.search_entry.pack(side=tk.LEFT, padx=5, expand=True, fill='x') search_button = tk.Button(search_frame, text="搜索", command=self.search_word, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 10), padx=15) search_button.pack(side=tk.LEFT, padx=5) # 模式选择按钮框架 mode_frame = tk.Frame(main_frame, bg=self.bg_color) mode_frame.pack(pady=10) # 练习模式按钮 self.practice_button = tk.Button(mode_frame, text="开始练习", command=self.toggle_practice_mode, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 12), padx=20, pady=5) self.practice_button.pack(side=tk.LEFT, padx=5) # 考试模式按钮 self.exam_button = tk.Button(mode_frame, text="开始考试", command=self.start_exam, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 12), padx=20, pady=5) self.exam_button.pack(side=tk.LEFT, padx=5) # 练习模式输入框(初始隐藏) self.practice_frame = tk.Frame(main_frame, bg=self.frame_bg, padx=10, pady=10) self.practice_frame.pack(fill='x', pady=10) self.answer_entry = tk.Entry(self.practice_frame, font=("微软雅黑", 12)) self.answer_entry.pack(side=tk.LEFT, padx=5, expand=True, fill='x') self.check_button = tk.Button(self.practice_frame, text="显示答案", command=self.check_answer, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 10), padx=15) self.check_button.pack(side=tk.LEFT, padx=5) # 添加提示标签 self.hint_label = tk.Label(self.practice_frame, text="按右键或回车进入下一题,按上键显示答案", font=("微软雅黑", 10), bg=self.frame_bg, fg="#666666") self.hint_label.pack(side=tk.LEFT, padx=5) # 添加上一题显示区域 self.last_word_frame = tk.Frame(main_frame, bg=self.frame_bg, padx=20, pady=20) self.last_word_frame.pack(fill='x', pady=10) self.last_word_label = tk.Label(self.last_word_frame, text="上一题:", font=("微软雅黑", 12, "bold"), bg=self.frame_bg) self.last_word_label.pack(anchor='w', pady=5) self.last_word_text = tk.Label(self.last_word_frame, text="", font=("微软雅黑", 12), bg=self.frame_bg) self.last_word_text.pack(anchor='w', pady=5) self.last_example_text = tk.Label(self.last_word_frame, text="", font=("微软雅黑", 10), bg=self.frame_bg, wraplength=400, justify='left') self.last_example_text.pack(anchor='w', pady=5) # 隐藏练习模式相关控件 self.practice_frame.pack_forget() # 单词显示区域 word_frame = tk.Frame(main_frame, bg=self.frame_bg, padx=20, pady=20) word_frame.pack(fill='x', pady=10) self.word_label = tk.Label(word_frame, text="", font=("微软雅黑", 24, "bold"), bg=self.frame_bg) self.word_label.pack(pady=10) self.meaning_label = tk.Label(word_frame, text="", font=("微软雅黑", 18), bg=self.frame_bg) self.meaning_label.pack(pady=10) self.example_label = tk.Label(word_frame, text="", font=("微软雅黑", 12), bg=self.frame_bg, wraplength=400, justify='left') self.example_label.pack(pady=10) # 按钮框架 button_frame = tk.Frame(main_frame, bg=self.bg_color) button_frame.pack(pady=20) # 创建按钮样式 button_style = { 'bg': self.button_color, 'fg': self.button_text_color, 'font': ("微软雅黑", 10), 'padx': 15, 'pady': 5 } self.show_button = tk.Button(button_frame, text="显示答案", command=self.show_answer, **button_style) self.show_button.pack(side=tk.LEFT, padx=5) self.next_button = tk.Button(button_frame, text="下一个", command=self.show_next_word, **button_style) self.next_button.pack(side=tk.LEFT, padx=5) self.edit_button = tk.Button(button_frame, text="编辑", command=self.edit_word, **button_style) self.edit_button.pack(side=tk.LEFT, padx=5) self.delete_button = tk.Button(button_frame, text="删除", command=self.delete_word, **button_style) self.delete_button.pack(side=tk.LEFT, padx=5) # 添加新单词的按钮 self.add_button = tk.Button(main_frame, text="添加新单词", command=self.show_add_word_window, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 12), padx=20, pady=5) self.add_button.pack(pady=10) def toggle_practice_mode(self): if self.exam_mode: self.exam_mode = False self.exam_button.config(text="开始考试") self.practice_frame.pack_forget() self.show_next_word() return self.practice_mode = not self.practice_mode if self.practice_mode: self.practice_button.config(text="退出练习") self.practice_frame.pack(fill='x', pady=10) self.word_label.pack_forget() self.show_button.pack_forget() self.show_next_word() else: self.practice_button.config(text="开始练习") self.practice_frame.pack_forget() self.word_label.pack(pady=20) self.show_button.pack(in_=self.show_button.master, side=tk.LEFT, padx=5) self.show_next_word() def show_next_word(self): if self.words: # 保存当前单词作为上一题 if self.current_word: self.last_word = self.current_word # 更新上一题显示 self.last_word_text.config(text=f"{self.last_word['word']} - {self.last_word['meaning']}") self.last_example_text.config(text=self.last_word['example']) self.current_word = random.choice(self.words) if self.practice_mode: self.meaning_label.config(text=self.current_word["meaning"]) self.example_label.config(text="") self.answer_entry.delete(0, tk.END) else: self.word_label.config(text=self.current_word["word"]) self.meaning_label.config(text="") self.example_label.config(text="") def show_answer(self): if self.current_word: self.meaning_label.config(text=self.current_word["meaning"]) self.example_label.config(text=self.current_word["example"]) def search_word(self): search_text = self.search_entry.get().strip().lower() if not search_text: messagebox.showwarning("警告", "请输入要搜索的单词!") return found_words = [word for word in self.words if search_text in word["word"].lower()] if not found_words: messagebox.showinfo("搜索结果", "未找到匹配的单词!") return # 创建搜索结果窗口 search_window = tk.Toplevel(self.root) search_window.title("搜索结果") search_window.geometry("300x400") # 创建列表框 listbox = tk.Listbox(search_window, width=40, height=15) listbox.pack(pady=10) # 添加搜索结果 for word in found_words: listbox.insert(tk.END, f"{word['word']} - {word['meaning']}") def show_selected_word(): selection = listbox.curselection() if selection: selected_word = found_words[selection[0]] self.current_word = selected_word self.word_label.config(text=selected_word["word"]) self.meaning_label.config(text=selected_word["meaning"]) self.example_label.config(text=selected_word["example"]) search_window.destroy() # 添加选择按钮 select_button = tk.Button(search_window, text="选择", command=show_selected_word) select_button.pack(pady=10) def edit_word(self): if not self.current_word: messagebox.showwarning("警告", "请先选择一个单词!") return # 创建编辑窗口 edit_window = tk.Toplevel(self.root) edit_window.title("编辑单词") edit_window.geometry("300x200") # 创建输入框 tk.Label(edit_window, text="单词:").pack() word_entry = tk.Entry(edit_window) word_entry.insert(0, self.current_word["word"]) word_entry.pack() tk.Label(edit_window, text="意思:").pack() meaning_entry = tk.Entry(edit_window) meaning_entry.insert(0, self.current_word["meaning"]) meaning_entry.pack() tk.Label(edit_window, text="例句:").pack() example_entry = tk.Entry(edit_window) example_entry.insert(0, self.current_word["example"]) example_entry.pack() def save_edit(): # 更新单词信息 self.current_word["word"] = word_entry.get() self.current_word["meaning"] = meaning_entry.get() self.current_word["example"] = example_entry.get() # 保存到文件 self.save_words() # 更新显示 self.word_label.config(text=self.current_word["word"]) self.meaning_label.config(text=self.current_word["meaning"]) self.example_label.config(text=self.current_word["example"]) messagebox.showinfo("成功", "单词修改成功!") edit_window.destroy() # 保存按钮 save_button = tk.Button(edit_window, text="保存", command=save_edit) save_button.pack(pady=10) def delete_word(self): if not self.current_word: messagebox.showwarning("警告", "请先选择一个单词!") return if messagebox.askyesno("确认", f"确定要删除单词 '{self.current_word['word']}' 吗?"): self.words.remove(self.current_word) self.save_words() self.show_next_word() messagebox.showinfo("成功", "单词删除成功!") def show_add_word_window(self): # 创建新窗口 add_window = tk.Toplevel(self.root) add_window.title("添加新单词") add_window.geometry("400x400") add_window.configure(bg=self.bg_color) # 创建输入框架 input_frame = tk.Frame(add_window, bg=self.frame_bg, padx=20, pady=20) input_frame.pack(expand=True, fill='both', padx=20, pady=20) # 创建输入框 tk.Label(input_frame, text="单词:", font=("微软雅黑", 12), bg=self.frame_bg).pack(pady=(0, 5)) word_entry = tk.Entry(input_frame, font=("微软雅黑", 12)) word_entry.pack(fill='x', pady=(0, 10)) tk.Label(input_frame, text="意思:", font=("微软雅黑", 12), bg=self.frame_bg).pack(pady=(0, 5)) meaning_entry = tk.Entry(input_frame, font=("微软雅黑", 12)) meaning_entry.pack(fill='x', pady=(0, 10)) tk.Label(input_frame, text="例句:", font=("微软雅黑", 12), bg=self.frame_bg).pack(pady=(0, 5)) example_entry = tk.Entry(input_frame, font=("微软雅黑", 12)) example_entry.pack(fill='x', pady=(0, 10)) def save_word(): new_word = { "word": word_entry.get(), "meaning": meaning_entry.get(), "example": example_entry.get() } self.words.append(new_word) self.save_words() messagebox.showinfo("成功", "单词添加成功!") add_window.destroy() # 保存按钮 save_button = tk.Button(input_frame, text="保存", command=save_word, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 12), padx=20, pady=5) save_button.pack(pady=10)
def check_answer(self): if not self.current_word: return user_answer = self.answer_entry.get().strip().lower() correct_answer = self.current_word["word"].lower() if self.exam_mode: # 保存用户答案 self.user_answers.append({ "word": self.current_word["word"], "meaning": self.current_word["meaning"], "user_answer": user_answer, "is_correct": user_answer == correct_answer }) if user_answer == correct_answer: self.correct_count += 1 self.current_exam_index += 1 self.show_exam_word() else: if user_answer == correct_answer: messagebox.showinfo("正确", "回答正确!") else: messagebox.showinfo("错误", f"回答错误!正确答案是:{self.current_word['word']}") self.example_label.config(text=self.current_word["example"]) self.show_next_word() # 自动进入下一题
def start_exam(self): if len(self.words) < 5: messagebox.showwarning("警告", "单词库中单词数量不足,请先添加更多单词!") return # 创建考试设置窗口 exam_window = tk.Toplevel(self.root) exam_window.title("考试设置") exam_window.geometry("300x300") exam_window.configure(bg=self.bg_color) # 创建输入框架 input_frame = tk.Frame(exam_window, bg=self.frame_bg, padx=20, pady=20) input_frame.pack(expand=True, fill='both', padx=20, pady=20) # 题目数量选择 tk.Label(input_frame, text="选择题目数量:", font=("微软雅黑", 12), bg=self.frame_bg).pack(pady=(0, 10)) num_frame = tk.Frame(input_frame, bg=self.frame_bg) num_frame.pack(pady=10) num_var = tk.StringVar(value="10") for num in ["5", "10", "15", "20"]: tk.Radiobutton(num_frame, text=num, variable=num_var, value=num, font=("微软雅黑", 12), bg=self.frame_bg).pack(side=tk.LEFT, padx=10) def start_exam_with_num(): num = int(num_var.get()) if num > len(self.words): messagebox.showwarning("警告", f"单词库中只有{len(self.words)}个单词,请选择更少的题目数量!") return self.exam_mode = True self.practice_mode = False self.exam_words = random.sample(self.words, num) self.current_exam_index = 0 self.correct_count = 0 # 更新界面 self.practice_button.config(text="开始练习") self.exam_button.config(text="退出考试") self.practice_frame.pack_forget() self.word_label.pack(pady=10) self.show_button.pack(in_=self.show_button.master, side=tk.LEFT, padx=5) # 显示第一题 self.show_exam_word() exam_window.destroy() # 开始考试按钮 start_button = tk.Button(input_frame, text="开始考试", command=start_exam_with_num, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 12), padx=20, pady=5) start_button.pack(pady=10)
def show_exam_word(self): if self.current_exam_index < len(self.exam_words): self.current_word = self.exam_words[self.current_exam_index] self.word_label.config(text=f"第 {self.current_exam_index + 1} 题") self.meaning_label.config(text=self.current_word["meaning"]) self.example_label.config(text="") self.answer_entry.delete(0, tk.END) self.practice_frame.pack(fill='x', pady=10) self.answer_entry.focus() # 自动聚焦到输入框 self.answer_shown = False # 重置答案显示状态 else: self.show_exam_result()
def show_exam_result(self): score = int(self.correct_count / len(self.exam_words) * 100) # 创建结果窗口 result_window = tk.Toplevel(self.root) result_window.title("考试结果") result_window.geometry("700x600") result_window.configure(bg=self.bg_color) # 显示结果 result_frame = tk.Frame(result_window, bg=self.frame_bg, padx=20, pady=20) result_frame.pack(expand=True, fill='both', padx=20, pady=20) # 显示总分 score_text = f"考试结束!\n\n总分:{score}分\n答对题目:{self.correct_count}/{len(self.exam_words)}" tk.Label(result_frame, text=score_text, font=("微软雅黑", 16, "bold"), bg=self.frame_bg, justify='center').pack(pady=10) # 创建详细答案列表 answer_frame = tk.Frame(result_frame, bg=self.frame_bg) answer_frame.pack(fill='both', expand=True, pady=10) # 添加表头 headers = ["题目", "中文", "你的答案", "正确答案", "结果"] for i, header in enumerate(headers): tk.Label(answer_frame, text=header, font=("微软雅黑", 12, "bold"), bg=self.frame_bg, width=10).grid(row=0, column=i, padx=5, pady=5) # 添加每道题的详细答案 for i, answer in enumerate(self.user_answers, 1): # 题目序号 tk.Label(answer_frame, text=str(i), font=("微软雅黑", 10), bg=self.frame_bg).grid(row=i, column=0, padx=5, pady=2) # 中文意思 tk.Label(answer_frame, text=answer["meaning"], font=("微软雅黑", 10), bg=self.frame_bg).grid(row=i, column=1, padx=5, pady=2) # 用户答案 tk.Label(answer_frame, text=answer["user_answer"], font=("微软雅黑", 10), bg=self.frame_bg).grid(row=i, column=2, padx=5, pady=2) # 正确答案 tk.Label(answer_frame, text=answer["word"], font=("微软雅黑", 10), bg=self.frame_bg).grid(row=i, column=3, padx=5, pady=2) # 结果(对/错) result_text = "✓" if answer["is_correct"] else "✗" result_color = "#4CAF50" if answer["is_correct"] else "#F44336" tk.Label(answer_frame, text=result_text, font=("微软雅黑", 12, "bold"), fg=result_color, bg=self.frame_bg).grid(row=i, column=4, padx=5, pady=2) # 关闭按钮 close_button = tk.Button(result_frame, text="关闭", command=result_window.destroy, bg=self.button_color, fg=self.button_text_color, font=("微软雅黑", 12), padx=20, pady=5) close_button.pack(pady=10) # 重置考试状态 self.exam_mode = False self.exam_button.config(text="开始考试") self.practice_frame.pack_forget() self.user_answers = [] # 清空答案记录 self.show_next_word()
if __name__ == "__main__": root = tk.Tk() app = VocabularyApp(root) root.mainloop()
|