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.

94 lines
2.7 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):
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()