6.13
python错题管理系统
import tkinter as tk from tkinter import ttk import pymysql class WrongQuestionManagementSystem: def __init__(self): self.root = tk.Tk() self.root.title("错题本信息管理系统") self.create_main_page() self.c = None def create_main_page(self): self.root.title("错题管理系统") title_label = tk.Label(self.root, text="错题管理系统", width=100, height=2) title_label.grid(row=0, column=0, columnspan=2) button1 = tk.Button(self.root, text="查询错题", command=self.create_page1, width=50, height=2) button1.grid(row=1, column=0) button2 = tk.Button(self.root, text="添加错题", command=self.create_page2, width=50, height=2) button2.grid(row=1, column=1) button3 = tk.Button(self.root, text="修改错题", command=self.create_page3, width=50, height=2) button3.grid(row=2, column=0) button4 = tk.Button(self.root, text="删除错题", command=self.create_page4, width=50, height=2) button4.grid(row=2, column=1) button5 = tk.Button(self.root, text="错题导出", command=self.create_page5, width=100, height=2) button5.grid(row=3, column=0, columnspan=2) def create_page1(self): # 创建新窗口 new_window = tk.Tk() new_window.title("错题查询") # 连接数据库 db_config = { 'host': 'localhost', 'user': 'root', 'password': '246800', 'database': 'db1' } db = pymysql.connect(**db_config) cursor = db.cursor() # 执行数据库查询 cursor.execute( "select question, error_info, analysis, experience, source, reason, difficulty, type, knowledge FROM wrong_questions;") data = cursor.fetchall() search_label = tk.Label(new_window, text="输入题目:") search_label.pack() search_entry = tk.Entry(new_window) search_entry.pack() # 创建Treeview表格 tree = ttk.Treeview(new_window) tree['columns'] = tuple(i[0] for i in cursor.description) tree.heading("#0", text="序号") for col, col_chinese in zip(tree['columns'], ["题目", "错误信息:", "答案分析:", "心得体会:", "错题来源:", "难易程度:", "题目类型:", "知识点:"]): tree.heading(col, text=col_chinese) tree.column(col, anchor=tk.CENTER) tree.column("#0", anchor=tk.CENTER) def search(): tree.delete(*tree.get_children()) query = search_entry.get().strip() if query: for row in data: if query in row: tree.insert('', 'end', values=row) else: for idx, row in enumerate(data, 1): tree.insert('', 'end', text=idx, values=row) search_button = tk.Button(new_window, text="搜索", command=search) search_button.pack() # 返回按钮 back_button = tk.Button(new_window, text="返回主页", command=new_window.destroy) back_button.pack() for idx, row in enumerate(data, 1): tree.insert('', 'end', text=idx, values=row) tree.pack() # 关闭数据库连接 db.close() new_window.mainloop() def create_page2(self): # Create content for page 2 # Create content for page 2 self.root.destroy() self.root = tk.Tk() self.root.title("错题添加") tk.Label(self.root, text="题目:",width=40,height=1).grid(row=0, column=0) self.question_entry = tk.Entry(self.root) self.question_entry.grid(row=0, column=1) tk.Label(self.root, text="错误信息:",width=40,height=1).grid(row=1, column=0) self.error_entry = tk.Entry(self.root) self.error_entry.grid(row=1, column=1) tk.Label(self.root, text="答案分析:",width=20,height=1).grid(row=2, column=0) self.analysis_entry = tk.Entry(self.root) self.analysis_entry.grid(row=2, column=1) tk.Label(self.root, text="心得体会:",width=20,height=1).grid(row=3, column=0) self.experience_entry = tk.Entry(self.root) self.experience_entry.grid(row=3, column=1) tk.Label(self.root, text="错题来源:",width=20,height=1).grid(row=4, column=0) self.source_entry = tk.Entry(self.root) self.source_entry.grid(row=4, column=1) tk.Label(self.root, text="错题原因:").grid(row=5, column=0) self.reason_entry = tk.Entry(self.root) self.reason_entry.grid(row=5, column=1) tk.Label(self.root, text="难易程度:",width=20,height=1).grid(row=6, column=0) self.difficulty_entry = tk.Entry(self.root) self.difficulty_entry.grid(row=6, column=1) tk.Label(self.root, text="题目类型:",width=20,height=1).grid(row=7, column=0) self.type_entry = tk.Entry(self.root) self.type_entry.grid(row=7, column=1) tk.Label(self.root, text="知识点:",width=20,height=1).grid(row=8, column=0) self.knowledge_entry = tk.Entry(self.root) self.knowledge_entry.grid(row=8, column=1) tk.Button(self.root, text="录入", command=self.add_wrong_question,width=80,height=1).grid(row=9, column=0, columnspan=2, pady=10) # 添加功能 back_button = tk.Button(self.root, text="返回主页面", command=self.create_main_page) back_button.grid(row=10, column=0, columnspan=2, pady=10) def add_wrong_question(self): question = self.question_entry.get() error = self.error_entry.get() analysis = self.analysis_entry.get() experience = self.experience_entry.get() source = self.source_entry.get() reason = self.reason_entry.get() difficulty = self.difficulty_entry.get() qtype = self.type_entry.get() knowledge = self.knowledge_entry.get() # 连接数据库 db_config = { 'host': 'localhost', 'user': 'root', 'password': '246800', 'database': 'db1' } db = pymysql.connect(**db_config) cursor = db.cursor() # 插入数据 sql = "INSERT INTO wrong_questions (question, error_info, analysis, experience, source, reason, difficulty, type, knowledge) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)" values = (question, error, analysis, experience, source, reason, difficulty, qtype, knowledge) cursor.execute(sql, values) # 提交更改 db.commit() # 关闭数据库连接 db.close() # 清空Entry部件 self.question_entry.delete(0, 'end') self.error_entry.delete(0, 'end') self.analysis_entry.delete(0, 'end') self.experience_entry.delete(0, 'end') self.source_entry.delete(0, 'end') self.reason_entry.delete(0, 'end') self.difficulty_entry.delete(0, 'end') self.type_entry.delete(0, 'end') self.knowledge_entry.delete(0, 'end') print("题目添加成功!") def create_page3(self): # Create content for page 2 self.root.destroy() self.root = tk.Tk() self.root.title("错题修改") tk.Label(self.root, text="题目:", width=40, height=1).grid(row=0, column=0) self.search_entry = tk.Entry(self.root) self.search_entry.grid(row=0, column=1) tk.Button(self.root, text="搜索", command=self.search_wrong_question, width=20, height=1).grid(row=0, column=2) back_button = tk.Button(self.root, text="返回主页面", command=self.create_main_page) back_button.grid(row=10, column=0, columnspan=2, pady=10) def search_wrong_question(self): question_to_search = self.search_entry.get() # 连接数据库 db_config = { 'host': 'localhost', 'user': 'root', 'password': '246800', 'database': 'db1' } db = pymysql.connect(**db_config) cursor = db.cursor() # 查询数据 sql = "select question, error_info, analysis, experience, source, reason, difficulty, type, knowledge FROM wrong_questions WHERE question = %s" cursor.execute(sql, (question_to_search,)) result = cursor.fetchone() db.close() if result: self.create_modify_form(result) else: print("题目不存在,请重新输入!") def modify_wrong_question(self): new_question = self.question_entry.get() new_error = self.error_entry.get() new_analysis = self.analysis_entry.get() new_experience = self.experience_entry.get() new_source = self.source_entry.get() new_reason = self.reason_entry.get() new_difficulty = self.difficulty_entry.get() new_type = self.type_entry.get() new_knowledge = self.knowledge_entry.get() db_config = { 'host': 'localhost', 'user': 'root', 'password': '246800', 'database': 'db1' } db = pymysql.connect(**db_config) cursor = db.cursor() sql = "UPDATE wrong_questions SET question = %s, error_info = %s, analysis = %s, experience = %s, source = %s, reason = %s, difficulty = %s, type = %s, knowledge = %s WHERE id = %s" # Assuming the ID of the question is 1 cursor.execute(sql, ( new_question, new_error, new_analysis, new_experience, new_source, new_reason, new_difficulty, new_type, new_knowledge, 1)) db.commit() tk.Label(self.root, text="题目已修改").grid(row=10, column=0, columnspan=2) def create_modify_form(self, question_details): self.root.destroy() self.root = tk.Tk() self.root.title("错题修改") tk.Label(self.root, text="题目:", width=40, height=1).grid(row=0, column=0) self.question_entry = tk.Entry(self.root) self.question_entry.grid(row=0, column=1) self.question_entry.insert(0, question_details[0]) tk.Label(self.root, text="错误信息:", width=40, height=1).grid(row=1, column=0) self.error_entry = tk.Entry(self.root) self.error_entry.grid(row=1, column=1) self.error_entry.insert(0, question_details[1]) tk.Label(self.root, text="答案分析:", width=20, height=1).grid(row=2, column=0) self.analysis_entry = tk.Entry(self.root) self.analysis_entry.grid(row=2, column=1) self.analysis_entry.insert(0, question_details[2]) tk.Label(self.root, text="心得体会:", width=20, height=1).grid(row=3, column=0) self.experience_entry = tk.Entry(self.root) self.experience_entry.grid(row=3, column=1) self.experience_entry.insert(0, question_details[3]) tk.Label(self.root, text="错题来源:", width=20, height=1).grid(row=4, column=0) self.source_entry = tk.Entry(self.root) self.source_entry.grid(row=4, column=1) self.source_entry.insert(0, question_details[4]) tk.Label(self.root, text="错题原因:").grid(row=5, column=0) self.reason_entry = tk.Entry(self.root) self.reason_entry.grid(row=5, column=1) self.reason_entry.insert(0, question_details[5]) tk.Label(self.root, text="难易程度:", width=20, height=1).grid(row=6, column=0) self.difficulty_entry = tk.Entry(self.root) self.difficulty_entry.grid(row=6, column=1) self.difficulty_entry.insert(0, question_details[6]) tk.Label(self.root, text="题目类型:", width=20, height=1).grid(row=7, column=0) self.type_entry = tk.Entry(self.root) self.type_entry.grid(row=7, column=1) self.type_entry.insert(0, question_details[7]) tk.Label(self.root, text="知识点:", width=20, height=1).grid(row=8, column=0) self.knowledge_entry = tk.Entry(self.root) self.knowledge_entry.grid(row=8, column=1) self.knowledge_entry.insert(0, question_details[8]) tk.Button(self.root, text="修改", command=self.modify_wrong_question, width=80, height=1).grid(row=9, column=0, columnspan=2, pady=10) def create_page4(self): self.root.destroy() self.root = tk.Tk() self.root.title("错题删除") def search_question(): question = self.search_entry.get() db_config = { 'host': 'localhost', 'user': 'root', 'password': '246800', 'database': 'db1' } db = pymysql.connect(**db_config) cursor = db.cursor() cursor.execute("SELECT * FROM wrong_questions WHERE question=%s", (question,)) data = cursor.fetchall() if data: result_label = tk.Label(self.root, text="找到题目:") result_label.pack() for row in data: tk.Label(self.root, text=row).pack() delete_button = tk.Button(self.root, text="删除题目", command=lambda: delete_question(question, cursor, db)) delete_button.pack() else: tk.Label(self.root, text="未找到题目").pack() def delete_question(question, cursor, db): cursor.execute("DELETE FROM wrong_questions WHERE question=%s", (question,)) db.commit() tk.Label(self.root, text="题目已删除").pack() # 创建搜索框和按钮 # 创建搜索框和按钮 tk.Label(self.root, text="输入题目:").pack() self.search_entry = tk.Entry(self.root) self.search_entry.pack() tk.Button(self.root, text="搜索", command=search_question).pack() self.root.mainloop() def create_page5(self, mysql=None): self.root = tk.Tk() self.root.title("错题导出") export_button = tk.Button(self.root, text="导出题目", command=self.export_questions) export_button.pack() self.root.mainloop() def export_questions(self): db_config = { 'host': 'localhost', 'user': 'root', 'password': '246800', 'database': 'db1' } db = pymysql.connect(**db_config) cursor = db.cursor() cursor.execute("SELECT question FROM wrong_questions") data = cursor.fetchall() with open("exported_questions.txt", "w") as file: for row in data: file.write(row[0] + "\n") cursor.close() tk.Label(self.root, text="题目已成功导出到 exported_questions.txt 文件").pack() def run(self): self.root.mainloop() if __name__ == "__main__": app = WrongQuestionManagementSystem() app.run()