You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.8 KiB

6 months ago
import sqlite3
import random
import tkinter as tk
from tkinter import Toplevel
# 数据库连接与初始化
def init_db_connection():
conn = sqlite3.connect('word_database.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS Words (word TEXT, meaning TEXT, learned INTEGER)')
conn.commit()
return conn
# 获取未学习的单词
def get_unlearned_word(conn):
cursor = conn.cursor()
cursor.execute('SELECT word, meaning FROM Words WHERE learned = 0 ORDER BY RANDOM() LIMIT 1')
result = cursor.fetchone()
if result:
return {'word': result[0], 'meaning': result[1]}
return None
# 标记单词为已学习
def mark_learned(word, conn):
cursor = conn.cursor()
cursor.execute('UPDATE Words SET learned = 1 WHERE word = ?', (word,))
conn.commit()
# 初始化主窗口
root = tk.Tk()
root.title("单词学习系统")
def open_new_window1(conn):
new_window = Toplevel(root)
new_window.title("新单词")
def learn_next_word():
word = get_unlearned_word(conn)
if word:
label_word.config(text=word['word'])
label_meaning.config(text=word['meaning'])
current_word = word['word']
else:
tk.messagebox.showinfo("提示", "没有更多新单词了!")
def review_previous():
# 实现回顾逻辑(这里简化处理,实际可能需要维护一个历史单词队列)
pass
current_word = get_unlearned_word(conn)
if current_word:
label_word = tk.Label(new_window, text=current_word['word'])
label_word.pack()
label_meaning = tk.Label(new_window, text=current_word['meaning'])
label_meaning.pack()
button_next = tk.Button(new_window, text="下一个单词", command=learn_next_word)
button_next.pack()
button_review = tk.Button(new_window, text="回顾", command=review_previous)
button_review.pack()
else:
tk.messagebox.showinfo("提示", "没有新单词!")
def open_new_window2(conn):
6 months ago
def check_spelling():
user_input = entry_spelling.get().strip()
if user_input.lower() == current_review_word['word']:
label_spelling.config(bg='green') # 标记为绿色表示正确
mark_next_review(conn) # 正确后标记下一个复习
else:
label_spelling.config(bg='red') # 错误则标记为红色
messagebox.showerror("错误", "拼写错误,请重试。")
def review_previous_word():
global current_review_word
# 实现回顾前一个单词的逻辑,这里简化处理,假设我们有一个全局变量或列表来追踪复习单词
# 实际应用中,可能需要维护复习单词的历史栈
pass
6 months ago
6 months ago
def mark_next_review(conn):
# 假设复习完当前单词后自动标记下一个为复习状态,实际逻辑可能更复杂
global current_review_word
next_word = get_learned_word(conn)
if next_word:
current_review_word = next_word
label_meaning.config(text=next_word['meaning'])
entry_spelling.delete(0, tk.END) # 清空输入框
label_spelling.config(text="", bg='white') # 重置标签颜色和内容
else:
messagebox.showinfo("提示", "没有更多复习单词了!")
new_window = Toplevel(root)
new_window.title("复习单词")
6 months ago
6 months ago
current_review_word = get_learned_word(conn)
if current_review_word:
label_meaning = tk.Label(new_window, text=current_review_word['meaning'], wraplength=200)
label_meaning.pack(pady=10)
label_spelling = tk.Label(new_window, text="", width=20, bg='white')
label_spelling.pack(pady=5)
entry_spelling = Entry(new_window)
entry_spelling.pack(pady=5)
button_check = tk.Button(new_window, text="检查拼写", command=check_spelling)
button_check.pack(side=tk.LEFT, padx=5)
button_prev = tk.Button(new_window, text="回顾前一个", command=review_previous_word)
button_prev.pack(side=tk.LEFT, padx=5)
else:
messagebox.showinfo("提示", "没有复习单词!")
6 months ago
new_window = Toplevel(root)
new_window.title("复习单词")
def open_new_window3(conn):
new_window = Toplevel(root)
new_window.title("易错单词")
# 同样,易错单词窗口逻辑需根据错误记录定制,这里简化处理
pass
button1 = tk.Button(root, text="学习新单词", command=lambda: open_new_window1(init_db_connection()))
button1.pack(pady=10)
button2 = tk.Button(root, text="复习单词", command=lambda: open_new_window2(init_db_connection()))
button2.pack(pady=10)
button3 = tk.Button(root, text="易错单词", command=lambda: open_new_window3(init_db_connection()))
button3.pack(pady=10)
root.mainloop()