I'm trying to build read-only table view which will be updated programmatically based on "id" of an entry. I choose Treeview as it fits the task, but I was able to update it only before mainloop() call or with the help of UI Button (which does not suit to my task). Here is the code:
import tkinter as tk
import tkinter.ttk as ttk
class InfoWindow:
root = None
treeview = None
def __init__(self):
self.info_window()
def add_info(self, name='test', text='TEST'):
self.treeview.insert('', 'end', name, text=text, values=("test value"))
def update_info(self, name, param, value):
self.treeview.set(name, param, value)
def info_window(self):
self.root = root_window('Info')
self.treeview = ttk.Treeview(self.root, columns=('name'))
self.treeview.column('name', width=200, anchor='center')
self.treeview.heading('name', text='Name')
self.treeview.pack()
self.add_info("1") #entry successfully added
tk.mainloop()
self.add_info("2") #entry was not added
def root_window(title="Title"):
root = tk.Tk()
root.title(title)
w = 800
h = 600
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
return root
info_root = InfoWindow()
info_root.add_info("test1", "TEST 1") #entry was not added
info_root.update_info("test1", "name", "test value")
I need Treeview to be updated without destroying window. UI is read-only with thousand of entries. Data is fetched from internet and processed to filter it and determine the diffs. Data updates happen few times a second. As soon as associated data for some "id" is change I need this change to be reflected in UI. Is it possible to use some kind of callbacks? Like when some "id" is updated it should call Treeview to refresh associated row.