Ⅶ、代码
main.py
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()