import tkinter as tk from tkinter import ttk, messagebox import db_connection from update import UpdatePage import time # 查询数据库并返回结果 def fetch_mistakes(search_text=""): conn1 = db_connection.create_connection() cursor = conn1.cursor() if search_text: query = "SELECT id, question, question_type, difficulty FROM mistakes WHERE question LIKE %s" cursor.execute(query, ('%' + search_text + '%',)) else: query = "SELECT id, question, question_type, difficulty FROM mistakes" cursor.execute(query) results = cursor.fetchall() cursor.close() return results # 搜索按钮点击事件处理 def on_search_click(): search_text = search_entry.get() mistakes = fetch_mistakes(search_text) display_results(mistakes) # 删除数据库记录的函数 def delete_mistake(id): conn1 = db_connection.create_connection() cursor = conn1.cursor() cursor.execute("DELETE FROM mistakes WHERE id = %s", (id,)) conn1.commit() cursor.close() # 查看详细信息的函数(弹出新窗口展示详情) def view_details_popup(conn, id): popup_window = tk.Toplevel() popup_window.title("题目详情") conn1 = db_connection.create_connection() cursor = conn1.cursor() # 查询该ID对应的完整记录 cursor.execute("SELECT * FROM mistakes WHERE id = %s", (id,)) detail = cursor.fetchone() cursor.close() if detail: # 构建详细信息展示 for i, column_name in enumerate(("ID", "题目", "错误信息", "答案分析", "心得体会", "错题来源", "错题原因", "难易程度", "题目类型", "知识点")): ttk.Label(popup_window, text=f"{column_name}: {detail[i]}").grid(row=i, column=0, sticky=tk.W, padx=5, pady=2) else: ttk.Label(popup_window, text="未找到相关记录").grid(row=0, column=0) # 修改信息的函数(此处仅为框架,具体UI及逻辑需根据需求实现) def modify_mistake_ui(root, id): # conn=db_connection.create_connection() print(f"Opening modify UI for mistake with ID: {id}") # 创建一个新的Tk实例,用于更新界面 update_root = tk.Toplevel(root) # 假设root是主窗口 update_root.title("修改错题信息") update_root.state("zoomed") # 创建并展示UpdatePage实例 update_page = UpdatePage(update_root, mistake_id=id) update_page.pack(fill=tk.BOTH, expand=True) def popup_action(event): item = tree.identify_row(event.y) if item: # 如果需要根据条件禁用或启用菜单项,应该在显示菜单前进行配置 # 例如,如果需要禁用删除命令,可以这样做: # popup_menu.entryconfig(1, state=tk.DISABLED) popup_menu.tk_popup(event.x_root, event.y_root) else: # 如果没有选中行,可以决定是否仍然弹出菜单或以其他方式处理 # 这里假设你想禁用所有命令并仍然弹出一个不可操作的菜单作为提示 for index in range(len(popup_menu.children)): popup_menu.entryconfigure(index, state=tk.DISABLED) popup_menu.tk_popup(event.x_root, event.y_root) # 记得在下次有效点击时恢复菜单项状态 for index in range(len(popup_menu.children)): popup_menu.entryconfigure(index, state=tk.NORMAL) # 主函数,创建Tkinter界面 def main(): global tree, search_entry, popup_menu root = tk.Tk() root.title("题目管理") root.state('zoomed') # 搜索框和按钮 search_label = ttk.Label(root, text="题目:") search_label.pack(pady=10) search_entry = ttk.Entry(root) search_entry.pack(pady=5) search_button = ttk.Button(root, text="查询", command=lambda: on_search_click()) search_button.pack(pady=5) # 表格展示数据 columns = ("ID", "题目", "题目类型", "难易程度") tree = ttk.Treeview(root, columns=columns, show="headings") for col in columns: tree.heading(col, text=col) tree.pack(fill="both", expand=True) # 为每一行添加按钮的右键菜单 popup_menu = tk.Menu(tree, tearoff=0) popup_menu.add_command(label="修改", command=lambda: modify_mistake_ui(root, tree.item(tree.selection())['values'][0])) popup_menu.add_command(label="删除", command=lambda: delete_and_refresh(tree.item(tree.selection())['values'][0])) popup_menu.add_separator() popup_menu.add_command(label="显示详细信息", command=lambda: view_details_popup(db_connection.create_connection(), tree.item(tree.selection())['values'][0])) tree.bind("<Button-3>", popup_action) # 绑定右键点击事件 conn1 = db_connection.create_connection() # 初始化数据展示 mistakes = fetch_mistakes() display_results(mistakes) root.mainloop() # 删除并刷新数据列表 def delete_and_refresh(id): delete_mistake(id) mistakes = fetch_mistakes() display_results(mistakes) # 显示结果 def display_results(results): for widget in tree.get_children(): tree.delete(widget) for result in results: tree.insert("", "end", values=result) if __name__ == "__main__": main()
标签:search,popup,tree,6.16,cursor,mistakes,root From: https://www.cnblogs.com/zzqq1314/p/18253459