diff --git a/pythonProject1/1.py b/pythonProject1/1.py new file mode 100644 index 0000000..4fea95f --- /dev/null +++ b/pythonProject1/1.py @@ -0,0 +1,43 @@ +import tkinter as tk +from tkinter import messagebox +import random +import pymysql +import test + + +class Window3(tk.Toplevel): + def __init__(self, master): + super().__init__(master) + self.title("易错单词复习") + self.geometry("400x300") + + # 数据库连接设置 + + self.cursor = self.db.cursor() + self.text_widget = tk.Text(self, wrap=tk.WORD, width=40, height=10) + self.text_widget.pack(expand=True, fill=tk.BOTH, padx=10, pady=10) + + # 获取易错单词 + self.fetch_and_display_mistaken_words() + + def fetch_and_display_mistaken_words(self): + self.cursor.execute("SELECT `word`, `meaning` FROM `words` WHERE `easily_mistaken` = 1") + mistaken_words = self.cursor.fetchall() + if mistaken_words: + self.display_words(mistaken_words) + else: + messagebox.showinfo("提示", "没有易错单词可供复习!") + self.destroy() + + def display_words(self, words): + # 使用Text控件显示单词和意义 + for idx, word_info in enumerate(words, start=1): + text_to_insert = f"{idx}. 单词: {word_info['word']}, 意义: {word_info['meaning']}\n" + self.text_widget.insert(tk.END, text_to_insert) + + # 自动滚动到底部 + self.text_widget.see(tk.END) + + def on_closing(self): + self.db.close() + self.destroy() \ No newline at end of file diff --git a/pythonProject1/__pycache__/test.cpython-312.pyc b/pythonProject1/__pycache__/test.cpython-312.pyc new file mode 100644 index 0000000..1c2bf3a Binary files /dev/null and b/pythonProject1/__pycache__/test.cpython-312.pyc differ diff --git a/pythonProject1/db.py b/pythonProject1/db.py new file mode 100644 index 0000000..177669f --- /dev/null +++ b/pythonProject1/db.py @@ -0,0 +1,20 @@ +import pymysql +def connect_db(): + # 数据库连接参数 + db_host = 'localhost' # 数据库主机名 + db_user = 'root' # 数据库用户名 + db_password = '123456' # 数据库密码 + db_database = 'user_info' # 数据库名 + db_port = 3306 # 数据库端口,通常默认为3306 + + # 创建连接 + connection = pymysql.connect(host=db_host, + user=db_user, + password=db_password, + database=db_database, + port=db_port) + cursor = connection.cursor() + return cursor +if __name__ == '__main__': + cursor = connect_db() + print("数据库连接成功!") \ No newline at end of file diff --git a/pythonProject1/test1.py b/pythonProject1/test1.py index 9b9f3c1..240cd65 100644 --- a/pythonProject1/test1.py +++ b/pythonProject1/test1.py @@ -208,7 +208,7 @@ class Window3(tk.Toplevel): self.title("易错单词复习") self.geometry("400x300") - # 初始化数据库连接 + # 数据库连接设置 self.db = pymysql.connect( host="localhost", # MySQL服务器地址 user="root", # 数据库用户名 @@ -218,88 +218,33 @@ class Window3(tk.Toplevel): cursorclass=pymysql.cursors.DictCursor # 使用字典游标,方便通过列名访问数据 ) self.cursor = self.db.cursor() + self.text_widget = tk.Text(self, wrap=tk.WORD, width=40, height=10) + self.text_widget.pack(expand=True, fill=tk.BOTH, padx=10, pady=10) # 获取易错单词 - 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 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.update_button_states() - - def show_next_word(self): - if self.current_index < self.total_words - 1: - self.current_index += 1 - else: - self.current_index = 0 - self.show_current_word() - - 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() + self.fetch_and_display_mistaken_words() - def update_button_states(self): - if self.current_index == 0: - self.button_previous.configure(state=tk.DISABLED) + def fetch_and_display_mistaken_words(self): + self.cursor.execute("SELECT `word`, `meaning` FROM `words` WHERE `easily_mistaken` = 1") + mistaken_words = self.cursor.fetchall() + if mistaken_words: + self.display_words(mistaken_words) 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() + messagebox.showinfo("提示", "没有易错单词可供复习!") + self.destroy() + def display_words(self, words): + # 使用Text控件显示单词和意义 + for idx, word_info in enumerate(words, start=1): + text_to_insert = f"{idx}. 单词: {word_info['word']}, 意义: {word_info['meaning']}\n" + self.text_widget.insert(tk.END, text_to_insert) -def main(): - # 初始化Tkinter的根窗口 - root = tk.Tk() - root.withdraw() # 可选:隐藏根窗口,如果你不打算显示它的话 + # 自动滚动到底部 + self.text_widget.see(tk.END) - # 创建并显示Window3实例 - window3 = Window3(root) - window3.protocol("WM_DELETE_WINDOW", window3.on_closing) - window3.mainloop() # 开始Window3窗口的事件循环 + def on_closing(self): + self.db.close() + self.destroy() def start_application(): root = tk.Tk()