首页 > 其他分享 >6.16 10

6.16 10

时间:2024-06-17 23:58:10浏览次数:25  
标签:10 val self 6.16 tk entry pady padx

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()

 

标签:10,val,self,6.16,tk,entry,pady,padx
From: https://www.cnblogs.com/zzqq1314/p/18253460

相关文章

  • 6.17 10
    <?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layou......
  • 6.16 9
    importtkinterastkfromtkinterimportttk,messageboximportdb_connectionfromupdateimportUpdatePageimporttime#查询数据库并返回结果deffetch_mistakes(search_text=""):conn1=db_connection.create_connection()cursor=conn1.cursor()......
  • 6.16 8
    importtkinterastkfromtkinterimportttk,messageboxfromPILimportImage,ImageTkimportmysql.connectorimportselect#数据库连接函数defcreate_connection():returnmysql.connector.connect(host='localhost',user='ro......
  • 每日一题-24-06-17 (P10218)(加倍!)
    看到异或直接想到线性基和trie很明显是trie从高到低一位位考虑,如果两个儿子都有,想使这一位为1,必须有一个变成加法然后就便利一下trie,记录一下剩余的体力和最小的加法的数就好了#include<bits/stdc++.h>usingnamespacestd;#definell__int128#definelstr[u][0]#define......
  • 6.16 2
    packagecom.zhen;importandroid.content.Intent;importandroid.util.Log;importandroid.widget.TextView;importandroid.widget.Toast;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroidx.core.text.HtmlCompat;import......
  • 6.16
    少爷放过我吧今天实现科技政策查询的前端,我前端设计的页面包括输入框,全国,河北省,外省的按钮,这样方便有代表性的查询。packagecom.zhen;importandroid.util.Log;importandroid.view.View;importandroid.widget.*;importandroidx.appcompat.app.AppCompatActivity;impo......
  • 6.16 3
    packagecom.example.mapper;importcom.example.pojo.Policy;importorg.apache.ibatis.annotations.Mapper;importorg.apache.ibatis.annotations.Select;importorg.w3c.dom.Text;importjava.util.List;@MapperpublicinterfacePolicyMapper{@Select(&q......
  • 6.16 5
    packagecom.example.controller;importcom.example.pojo.Policy;importcom.example.server.PolicyServer;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.w......
  • 6.16 4
    packagecom.example.server;importcom.example.mapper.PolicyMapper;importcom.example.pojo.Policy;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@Servicepubliccla......
  • 6.16 7
    exportdefault{data(){return{currentDisplayCount:2,searchItems:[{field:'name',label:'政策名称',value:'',logicalOperator:'AND',matchType:'精确'},{field:'......