You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何无需重启程序实现open_statistics窗口数据实时更新?

解决方案

问题核心在于你当前的table_data是程序启动时仅加载一次的静态数据,数据库更新后不会自动同步,且open_statistics窗口一旦创建就不会重新拉取数据。以下是具体修复方案:

1. 移除静态数据变量,每次按需查询数据库

修改connection.py,删掉全局的静态table_data赋值,只保留数据库查询函数:

def tuple_selection():
    with connect(file_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM statistics")
        records = cursor.fetchall()
        employee_tuples_list = [tuple(record) for record in records]
        return employee_tuples_list
# 移除这行静态赋值:table_data = tuple_selection()

2. 修改统计窗口逻辑,支持手动/自动刷新

修改interface.py,让统计窗口可以拉取最新数据并更新表格,同时添加手动刷新按钮和可选的自动刷新功能:

import tkinter as tk
from tkinter import ttk
from connection import tuple_selection  # 导入数据库查询函数

def show_statistics():
    open_statistics()  # 不再传入静态数据,改为内部实时拉取

def open_statistics():
    # 用Toplevel代替Tk,避免多主窗口引发的异常
    statistics = tk.Toplevel()
    statistics.geometry("950x450")  # 预留空间放置刷新按钮
    statistics.title("Statistics")
    statistics.resizable(width=False, height=False)

    heads = ["DATE VALUE", "TIME VALUE", "TOURNAMENT NAME", "BUY-IN", "QUANTITY BUY-IN", "PLAYER COUNT", "TOURNAMENT PLACE", "GAIN"]  
    table = ttk.Treeview(statistics, show="headings")  
    table["columns"] = heads

    def sort_column(tree, col, reverse):
        # 保持原排序逻辑不变,基于当前表格数据排序
        data = [(float(tree.set(child, col)), child) for child in tree.get_children('')]
        data.sort(reverse=reverse)
        for index, (value, child) in enumerate(data):
            tree.move(child, '', index)
        tree.heading(col, command=lambda: sort_column(tree, col, not reverse))

    def refresh_data():
        # 清空表格旧数据
        for item in table.get_children():
            table.delete(item)
        # 拉取数据库最新数据
        latest_data = tuple_selection()
        # 插入新数据到表格
        for row in latest_data:
            table.insert('', tk.END, values=row)

    # 配置表格列头和列宽
    for header in heads:
        table.heading(header, text=header, anchor="center", command=lambda col=header: sort_column(table, col, False))
        table.column(header, anchor="center", width=100)

    # 添加手动刷新按钮
    refresh_btn = ttk.Button(statistics, text="刷新数据", command=refresh_data)
    refresh_btn.pack(side=tk.TOP, fill=tk.X, padx=10, pady=5)

    # 配置滚动条
    scrollpane = ttk.Scrollbar(statistics, command=table.yview)
    scrollpane.pack(side=tk.RIGHT, fill=tk.Y)
    table.pack(expand=tk.YES, fill=tk.BOTH)
    table.configure(yscrollcommand=scrollpane.set)

    # 可选:自动刷新(每5秒执行一次,单位毫秒)
    def auto_refresh():
        refresh_data()
        statistics.after(5000, auto_refresh)  # 递归调用实现持续自动刷新

    # 开启自动刷新(不需要的话注释掉这行)
    auto_refresh()

    # 窗口初始化时加载一次数据
    refresh_data()

关键修改说明

  • 用Toplevel替代Tk创建统计窗口:Tkinter仅允许一个主窗口,子窗口用Toplevel更规范,避免异常。
  • 新增refresh_data函数:负责清空旧数据、拉取最新数据库数据、重新渲染表格,是实现数据更新的核心。
  • 移除静态数据依赖:每次刷新都直接调用数据库查询函数,确保获取的是最新数据。
  • 自动刷新逻辑:通过after方法定时触发刷新,解决你之前定时器未生效的问题(之前的定时器可能未重新查询数据库或更新表格)。

内容的提问来源于stack exchange,提问作者TURRA7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:41:01