首页 > 其他分享 >6.16 8

6.16 8

时间:2024-06-17 23:43:11浏览次数:10  
标签:val self 6.16 tk pady padx pack

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


# 数据库连接函数
def create_connection():
    return mysql.connector.connect(
        host='localhost',
        user='root',  # 使用你的数据库用户名
        password='123456789',  # 使用你的数据库密码
        database='MistakeDB'
    )


# 插入数据到数据库的函数
def insert_mistake(conn, question, error_info, answer_analysis, thoughts, source, reason, difficulty, question_type,
                   knowledge_point):
    cursor = conn.cursor()
    sql = (
        "INSERT INTO mistakes (question, error_info, answer_analysis, thoughts, source, reason, difficulty, question_type, knowledge_point) "
        "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
    val = (question, error_info, answer_analysis, thoughts, source, reason, difficulty, question_type, knowledge_point)
    cursor.execute(sql, val)
    conn.commit()
    cursor.close()


# 主界面类
class MainInterface(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("错题本信息管理系统")
        self.state('zoomed')
        self.setup_background()
        self.create_widgets()

        # 初始化输入页面Frame,但不显示
        self.input_page_frame = None

    def setup_background(self):
        """设置背景图片"""
        self.background_image = Image.open("background.jpg")  # 确保图片存在并正确命名
        self.background_image = self.background_image.resize((self.winfo_screenwidth(), self.winfo_screenheight()),
                                                             resample=Image.Resampling.LANCZOS)
        self.background_tk = ImageTk.PhotoImage(self.background_image)
        background_label = tk.Label(self, image=self.background_tk)
        background_label.image = self.background_tk
        background_label.place(relwidth=1, relheight=1)

    def create_widgets(self):
        """创建主界面控件"""
        main_frame = tk.Frame(self, bg="lightgray")
        main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)

        title_font = ("Helvetica", 36, "bold")
        title_label = tk.Label(main_frame, text="错题本信息管理系统", font=title_font, bg="lightgray", pady=20)
        title_label.pack(pady=20)

        buttons_frame = tk.Frame(main_frame)
        buttons_frame.pack(expand=True, pady=(0, 20))

        add_button = tk.Button(buttons_frame, text="错题录入", command=self.show_input_page, font=("Helvetica", 18),
                               width=20)
        add_button.pack(fill=tk.X, pady=50)

        query_button = tk.Button(buttons_frame, text="查询显示", command=self.open_query_page, font=("Helvetica", 18),
                                 width=20)
        query_button.pack(fill=tk.X, pady=50)

    def show_input_page(self):
        """显示错题录入页面"""
        if self.input_page_frame is None:
            self.input_page_frame = InputPage(self)
        self.input_page_frame.pack(fill=tk.BOTH, expand=True)
        self.hide_main_widgets()

    def hide_input_page(self):
        """隐藏错题录入页面"""
        if self.input_page_frame:
            self.input_page_frame.pack_forget()
            self.show_main_widgets()

    def hide_main_widgets(self):
        """隐藏主界面控件"""
        for widget in self.winfo_children():
            if widget != self.input_page_frame:
                widget.pack_forget()

    def show_main_widgets(self):
        """显示主界面控件"""
        main_frame = self.winfo_children()[1]  # Assuming main frame is the second child after background label
        main_frame.pack(fill=tk.BOTH, expand=True)

    def open_query_page(self):
        """打开查询显示页面的占位函数"""
        print("打开查询显示页面")
        # 这里可以添加打开查询显示页面的代码
        select.main()


# 错题录入页面类
# 错题录入页面类
class InputPage(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.configure(bg="lightgray")
        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)

    def return_to_main(self):
        """返回到主界面"""
        self.master.hide_input_page()  # 隐藏错题录入页面
        self.master.show_main_widgets()  # 显示主界面控件

    def save_and_close(self):
        """保存错题信息并返回主界面"""
        # 获取所有输入控件的数据
        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 = create_connection()
            if conn.is_connected():
                insert_mistake(conn, 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.entry_question.delete("1.0", 'end')
        self.entry_error_info.delete("1.0", 'end')
        self.entry_answer_analysis.delete("1.0", 'end')
        self.entry_thoughts.delete("1.0", 'end')
        self.entry_source.delete("1.0", 'end')
        self.entry_reason.delete("1.0", 'end')
        self.entry_knowledge_point.delete("1.0", 'end')

        self.master.hide_input_page()

# 运行应用
if __name__ == "__main__":
    app = MainInterface()
    app.mainloop()

 

标签:val,self,6.16,tk,pady,padx,pack
From: https://www.cnblogs.com/zzqq1314/p/18253458

相关文章

  • 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:'......
  • 6.16 6
    今天实现高级检索的html代码<template><divid="search-container"><divclass="search-row"v-for="(item,index)inlimitedSearchItems":key="index"><divclass=&......
  • 6.16 学习心得
    《梦断代码》一书记录的是作者罗森伯格对OSAF主持的Chandler项目进行田野调查,通过Chandler开发过程来揭示软件开发过程中一些根本性的大问题。对本书才刚刚阅读了三分之一,就已经忍不住对作者描述的开发过程所感叹,虽然刚进入软件领域不久,但是仍旧有感于这本书中的现实,对自己也很有......
  • Java 6.16 DeepClone and ShallowClone
    浅克隆:复制对象的引用地址,导致克隆对象和原始对象共享引用类型字段的实际对象。classPersonimplementsCloneable{Stringname;Addressaddress;publicPerson(Stringname,Addressaddress){this.name=name;this.address=add......
  • 2024.6.16
    publicclassSparkSQL09_Source_Req{publicstaticvoidmain(String[]args){//TODO在编码前,设定Hadoop的访问用户System.setProperty("HADOOP_USER_NAME","atguigu");finalSparkSessionsparkSession=SparkSession......