From a04ca907a8347f8cc8d4e171ad90f83da02b3080 Mon Sep 17 00:00:00 2001 From: gyt200300 <2761891041@qq.com> Date: Sun, 2 Jun 2024 23:53:03 +0800 Subject: [PATCH] 6/2 --- pythonProject1/test1.py | 117 +++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 32 deletions(-) diff --git a/pythonProject1/test1.py b/pythonProject1/test1.py index a3479db..9b9f3c1 100644 --- a/pythonProject1/test1.py +++ b/pythonProject1/test1.py @@ -206,47 +206,100 @@ class Window3(tk.Toplevel): def __init__(self, master): super().__init__(master) self.title("易错单词复习") - self.conn = pymysql.connect( - host="localhost", - user="root", - password="21412030117", - database="word", - charset='utf8mb4', - cursorclass=pymysql.cursors.DictCursor + self.geometry("400x300") + + # 初始化数据库连接 + self.db = pymysql.connect( + host="localhost", # MySQL服务器地址 + user="root", # 数据库用户名 + password="21412030117", # 数据库密码 + database="word", # 数据库名 + charset='utf8mb4', # 字符编码,根据需要调整 + cursorclass=pymysql.cursors.DictCursor # 使用字典游标,方便通过列名访问数据 ) - self.cursor = self.conn.cursor() - self.setup_ui() - self.show_next_mistaken_word() # 初始化显示一个易错单词 + self.cursor = self.db.cursor() + + # 获取易错单词 + 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): - self.word_label = tk.Label(self, text="", font=("Arial", 16)) - self.word_label.pack(pady=10) + def show_current_word(self): + if self.total_words > 0: + 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): - self.cursor.execute("SELECT `word`, `meaning` FROM `words` WHERE `easily_mistaken` = 1 ORDER BY RAND() LIMIT 1") - word_row = self.cursor.fetchone() - if word_row: - self.current_word = word_row['word'] - self.current_meaning = word_row['meaning'] - self.word_label.config(text=f"含义:{self.current_meaning}") + def show_next_word(self): + if self.current_index < self.total_words - 1: + self.current_index += 1 else: - messagebox.showinfo("提示", "没有更多易错单词可供复习!") - self.destroy() + self.current_index = 0 + self.show_current_word() - def on_closing(self): - self.conn.close() - self.destroy() + def show_previous_word(self): + if self.current_index > 0: + self.current_index -= 1 + else: + self.current_index = self.total_words - 1 + self.show_current_word() - def protocol(self, event): - self.on_closing() + def update_button_states(self): + 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(root) -#window3.protocol("WM_DELETE_WINDOW", window3.on_closing) + # 创建并显示Window3实例 + window3 = Window3(root) + window3.protocol("WM_DELETE_WINDOW", window3.on_closing) + window3.mainloop() # 开始Window3窗口的事件循环 def start_application(): root = tk.Tk()