sqlite 如何使数据不断更新而不必重新启动?

yr9zkbsy  于 2023-08-06  发布在  SQLite
关注(0)|答案(1)|浏览(146)

在我的申请中:

  • 函数tuple_selection从数据库中的表中获取数据,并将其转换为卡片列表,然后将其传递给变量table_data
  • 函数show_statistics调用open_statistics,该函数接受带有墨盒的变量table_data
  • 函数open_statistics创建一个窗口,在其中构建一个表并将数据从table_data传递到其中。
  • 该程序有一个主窗口(在其中填写表单并保存到数据库)和一个打开open_statistics并在表中显示此数据的窗口。

如何使open_statistics中的数据不断更新,而不必不断重新启动程序?
网址:connection.py

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()

字符串
网址:interface.py

def show_statistics():
    open_statistics(table_data)

def open_statistics(data):
    statistics = tk.Tk()
    statistics.geometry("950x400")
    statistics.title("Statistics")
    statistics.resizable(width=False, height=False)

    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))

    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
    for row in 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)

    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)


我已经尝试过在定时器上调用函数以及在这些函数内部调用函数。

euoag5mw

euoag5mw1#

您可以使用.after()调用一个函数,该函数定期刷新表。请注意,您需要调用tuple_selection()来获取该函数内部的数据,而不是通过open_statistics()的参数传递表数据:

def open_statistics():
    statistics = tk.Toplevel() # use tk.Toplevel for child window
    statistics.geometry("950x400")
    statistics.title("Statistics")
    statistics.resizable(width=False, height=False)

    def refresh_table():
        # clear current table
        table.delete(*table.get_children())
        # fetch the table data
        data = tuple_selection()
        # populate the data into the table
        for row in data:
            table.insert('', tk.END, values=row)
        # refresh table after 5000 ms (change it to whatever you want)
        table.after(5000, refresh_table)

    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))

    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

    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)

    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)

    refresh_table() # start the periodic refresh

字符串
还要注意,对于根/主窗口以外的窗口,最好使用tk.Toplevel而不是tk.Tk

相关问题