gyt200300 6 months ago
parent ae23f47c35
commit a04ca907a8

@ -206,47 +206,100 @@ class Window3(tk.Toplevel):
def __init__(self, master): def __init__(self, master):
super().__init__(master) super().__init__(master)
self.title("易错单词复习") self.title("易错单词复习")
self.conn = pymysql.connect( self.geometry("400x300")
host="localhost",
user="root", # 初始化数据库连接
password="21412030117", self.db = pymysql.connect(
database="word", host="localhost", # MySQL服务器地址
charset='utf8mb4', user="root", # 数据库用户名
cursorclass=pymysql.cursors.DictCursor password="21412030117", # 数据库密码
database="word", # 数据库名
charset='utf8mb4', # 字符编码,根据需要调整
cursorclass=pymysql.cursors.DictCursor # 使用字典游标,方便通过列名访问数据
) )
self.cursor = self.conn.cursor() self.cursor = self.db.cursor()
self.setup_ui()
self.show_next_mistaken_word() # 初始化显示一个易错单词 # 获取易错单词
self.fetch_mistaken_words()
# UI设置
self.word_label = tk.Label(self, text="", font=("Arial", 14))
self.word_label.pack(pady=20)
# 按钮
button_frame = tk.Frame(self)
button_frame.pack(pady=10)
tk.Button(button_frame, text="上一个单词", command=self.show_previous_word, state=tk.DISABLED).pack(
side=tk.LEFT, padx=10)
tk.Button(button_frame, text="下一个单词", command=self.show_next_word).pack(side=tk.RIGHT, padx=10)
# 初始化显示第一个单词
self.show_current_word()
def fetch_mistaken_words(self):
self.cursor.execute("SELECT `word`, `meaning` FROM `words` WHERE `easily_mistaken` = 1 ORDER BY `id`")
self.mistaken_words = self.cursor.fetchall()
self.current_index = 0
self.total_words = len(self.mistaken_words)
if self.total_words == 0:
messagebox.showinfo("提示", "没有易错单词可供复习!")
self.destroy()
def setup_ui(self): def show_current_word(self):
self.word_label = tk.Label(self, text="", font=("Arial", 16)) if self.total_words > 0:
self.word_label.pack(pady=10) word_info = self.mistaken_words[self.current_index]
self.word_label.config(text=f"单词: {word_info['word']}, 意义: {word_info['meaning']}")
self.next_button = tk.Button(self, text="下一个单词", command=self.show_next_mistaken_word) # 更新按钮状态
self.next_button.pack(pady=10) self.update_button_states()
def show_next_mistaken_word(self): def show_next_word(self):
self.cursor.execute("SELECT `word`, `meaning` FROM `words` WHERE `easily_mistaken` = 1 ORDER BY RAND() LIMIT 1") if self.current_index < self.total_words - 1:
word_row = self.cursor.fetchone() self.current_index += 1
if word_row:
self.current_word = word_row['word']
self.current_meaning = word_row['meaning']
self.word_label.config(text=f"含义:{self.current_meaning}")
else: else:
messagebox.showinfo("提示", "没有更多易错单词可供复习!") self.current_index = 0
self.destroy() self.show_current_word()
def on_closing(self): def show_previous_word(self):
self.conn.close() if self.current_index > 0:
self.destroy() self.current_index -= 1
else:
self.current_index = self.total_words - 1
self.show_current_word()
def protocol(self, event): def update_button_states(self):
self.on_closing() if self.current_index == 0:
self.button_previous.configure(state=tk.DISABLED)
else:
self.button_previous.configure(state=tk.NORMAL)
if self.current_index == self.total_words - 1:
self.button_next.configure(state=tk.DISABLED)
else:
self.button_next.configure(state=tk.NORMAL)
def on_closing(self):
# 关闭窗口前的清理工作,如关闭数据库连接
self.db.close()
super().destroy()
# 实例化Window3的正确位置
if __name__ == "__main__":
root = tk.Tk()
root.withdraw() # 如果需要隐藏主窗口
window3 = Window3(root)
window3.protocol("WM_DELETE_WINDOW", window3.on_closing)
window3.mainloop()
def main():
# 初始化Tkinter的根窗口
root = tk.Tk()
root.withdraw() # 可选:隐藏根窗口,如果你不打算显示它的话
# 在创建Window3实例的地方确保调用protocol方法绑定关闭事件 # 创建并显示Window3实例
#window3 = Window3(root) window3 = Window3(root)
#window3.protocol("WM_DELETE_WINDOW", window3.on_closing) window3.protocol("WM_DELETE_WINDOW", window3.on_closing)
window3.mainloop() # 开始Window3窗口的事件循环
def start_application(): def start_application():
root = tk.Tk() root = tk.Tk()

Loading…
Cancel
Save