6.16 10

import tkinter as tk
from tkinter import ttk, messagebox
from PIL import Image, ImageTk
import mysql.connector

import db_connection


def update_mistake(conn, mistake_id, question, error_info, answer_analysis, thoughts, source, reason, difficulty,
                   question_type, knowledge_point):
    try:
        cursor = conn.cursor()
        # 构建SQL更新语句
        sql_query = """
        UPDATE mistakes 
        SET 
            question = %s, 
            error_info = %s, 
            answer_analysis = %s, 
            thoughts = %s, 
            source = %s, 
            reason = %s, 
            difficulty = %s, 
            question_type = %s, 
            knowledge_point = %s
        WHERE 
            id = %s
        """

        # 执行更新操作
        cursor.execute(sql_query, (question, error_info, answer_analysis, thoughts, source, reason, difficulty,
                                   question_type, knowledge_point, mistake_id))
        # 提交事务
        conn.commit()

        cursor.close()

        print("错题信息更新成功!")
    except mysql.connector.Error as err:
        print(f"更新错题信息时发生错误: {err}")
        # 在实际应用中,可能还需要进行回滚操作 `conn.rollback()` 和进一步的错误处理
    finally:
        pass


class UpdatePage(tk.Frame):
    def __init__(self, master=None, mistake_id=None):
        super().__init__(master)
        self.mistake_id = mistake_id
        self.create_widgets()
        global conn
        conn = db_connection.create_connection()

    def create_widgets(self):
        self.create_input_widgets()

    def create_input_widgets(self):
        padx_val = 10
        pady_val = 5

        # 题目输入框
        tk.Label(self, text="题目:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_question = tk.Text(self, width=50, height=2, wrap=tk.WORD)
        self.entry_question.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 错误信息输入框
        tk.Label(self, text="错误信息:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_error_info = tk.Text(self, width=50, height=2, wrap=tk.WORD)
        self.entry_error_info.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 答案分析输入框
        tk.Label(self, text="答案分析:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_answer_analysis = tk.Text(self, width=50, height=2, wrap=tk.WORD)
        self.entry_answer_analysis.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 心得体会输入框
        tk.Label(self, text="心得体会:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_thoughts = tk.Text(self, width=50, height=2, wrap=tk.WORD)
        self.entry_thoughts.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 来源输入框
        tk.Label(self, text="来源:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_source = tk.Text(self, width=50, height=2, wrap=tk.WORD)
        self.entry_source.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 原因输入框
        tk.Label(self, text="原因:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_reason = tk.Text(self, width=50, height=2, wrap=tk.WORD)
        self.entry_reason.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 难易程度下拉菜单
        tk.Label(self, text="难易程度:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        difficulty_options = ["简单", "中等", "困难"]
        self.difficulty_var = tk.StringVar()
        self.difficulty_menu = ttk.Combobox(self, textvariable=self.difficulty_var, values=difficulty_options)
        self.difficulty_menu.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 题目类型下拉菜单
        tk.Label(self, text="题目类型:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        type_options = ["选择题", "填空题", "解答题"]
        self.type_var = tk.StringVar()
        self.type_menu = ttk.Combobox(self, textvariable=self.type_var, values=type_options)
        self.type_menu.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 知识点输入框
        tk.Label(self, text="知识点:", bg="lightgray").pack(anchor=tk.W, padx=padx_val, pady=pady_val)
        self.entry_knowledge_point = tk.Text(self, width=50, height=3, wrap=tk.WORD)
        self.entry_knowledge_point.pack(anchor=tk.W, padx=padx_val, pady=pady_val)

        # 保存按钮
        tk.Button(self, text="保存并返回", command=self.save_and_close).pack(side=tk.BOTTOM, pady=20)
        # 保存按钮
        tk.Button(self, text="返回", command=self.return_to_main).pack(side=tk.BOTTOM, pady=20)

        # 添加一个查询数据库并填充表单的方法
        self.fetch_data()

    def fetch_data(self):
        conn = db_connection.create_connection()
        try:
            cursor = conn.cursor()
            query = "SELECT question, error_info, answer_analysis, thoughts, source, reason, difficulty, question_type, knowledge_point FROM mistakes WHERE id = %s"
            cursor.execute(query, (self.mistake_id,))
            data = cursor.fetchone()
            cursor.close()

            if not data:
                messagebox.showerror("错误", "未找到该记录")
                return
            # 填充表单
            self.entry_question.insert(tk.END, data[0])
            self.entry_error_info.insert(tk.END, data[1])
            self.entry_answer_analysis.insert(tk.END, data[2])
            self.entry_thoughts.insert(tk.END, data[3])
            self.entry_source.insert(tk.END, data[4])
            self.entry_reason.insert(tk.END, data[5])
            # 设置下拉菜单值
            self.difficulty_var.set(data[6])
            self.type_var.set(data[7])

            self.entry_knowledge_point.insert(tk.END, data[8])
        except mysql.connector.Error as err:
            messagebox.showerror("数据库错误", f"查询失败: {err}")

    def save_and_close(self):
        """保存错题信息并返回主界面"""
        # 获取所有输入控件的数据
        global conn
        question = self.entry_question.get("1.0", 'end-1c')
        error_info = self.entry_error_info.get("1.0", 'end-1c')
        answer_analysis = self.entry_answer_analysis.get("1.0", 'end-1c')
        thoughts = self.entry_thoughts.get("1.0", 'end-1c')
        source = self.entry_source.get("1.0", 'end-1c')
        reason = self.entry_reason.get("1.0", 'end-1c')
        difficulty = self.difficulty_var.get()
        question_type = self.type_var.get()
        knowledge_point = self.entry_knowledge_point.get("1.0", 'end-1c')

        try:
            conn = db_connection.create_connection()
            if conn.is_connected():
                update_mistake(conn, self.mistake_id, question, error_info, answer_analysis, thoughts, source, reason,
                               difficulty,
                               question_type, knowledge_point)
                messagebox.showinfo("成功", "错题信息已更新!")
            else:
                messagebox.showerror("错误", "无法连接到数据库!")
        except mysql.connector.Error as err:
            messagebox.showerror("数据库错误", f"发生错误: {err}")
        finally:
            if conn and conn.is_connected():
                conn.close()
        self.return_to_main()

    def return_to_main(self):
        self.master.destroy()

 

posted @ 2024-06-17 23:42  七安。  阅读(10)  评论(0)    收藏  举报