首页 > 其他分享 >6.16 9

6.16 9

时间:2024-06-17 23:43:31浏览次数:23  
标签:search popup tree 6.16 cursor mistakes root

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

相关文章

  • 6.16 8
    importtkinterastkfromtkinterimportttk,messageboxfromPILimportImage,ImageTkimportmysql.connectorimportselect#数据库连接函数defcreate_connection():returnmysql.connector.connect(host='localhost',user='ro......
  • 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......