在我的申请中:
- 函数
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)
型
我已经尝试过在定时器上调用函数以及在这些函数内部调用函数。
1条答案
按热度按时间euoag5mw1#
您可以使用
.after()
调用一个函数,该函数定期刷新表。请注意,您需要调用tuple_selection()
来获取该函数内部的数据,而不是通过open_statistics()
的参数传递表数据:字符串
还要注意,对于根/主窗口以外的窗口,最好使用
tk.Toplevel
而不是tk.Tk
。