0

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.

dandepeched
  • 424
  • 5
  • 20
  • 1
    `mainloop` is a blocking method, it pretty much runs a `while True:` loop, until you quit it (close the root window) – Matiiss Dec 26 '21 at 22:29
  • @Matiiss how to update displayed info without destroying window? In my app there will be thousands of entries updated randomly each second, so I need to refresh only ids that were changed. – dandepeched Dec 26 '21 at 22:43
  • using callbacks, do you want that to happen automatically? so that it kinda happens in the background? either `threading` and `after` "loops" or if updating doesn't take much time then only `after` "loops". They basically allow to schedule a function call and if you schedule the same function in itself, then you create a kind of a loop. – Matiiss Dec 26 '21 at 22:47
  • @Matiiss, I need it to be updated via callback, not a timer like 'after'. Is it possible? When some id in the app will be updated it should call tkinter to refresh associated data. Please post such code and I will accept it as answer. – dandepeched Dec 26 '21 at 22:57
  • would be great if you provided how anything would be updated, like does it pull data from somewhere periodically? how? user input? also `after` can be used as a loop that constantly updates what needs to be updated, not a timer, just constant checking for updates – Matiiss Dec 26 '21 at 23:00
  • @Matiiss application is read-only. 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 entry is change I need this change to be reflected in UI. – dandepeched Dec 27 '21 at 08:23
  • does this help: https://stackoverflow.com/questions/24849265/how-do-i-create-an-automatically-updating-gui-using-tkinter you would jus tneed to instead of configuring some label, insert new data in the treeview – Matiiss Dec 27 '21 at 12:06

0 Answers0