widget in sub-window update with real-time data in tkinter python

2024/10/12 16:26:39

I've tried using the after/time.sleep to update the treeview, but it is not working with the mainloop.

My questions are:

  • How can I update the treeview widget with real-time data?
  • And is there a way that I can change all treeview children simultaneously? (I don't want to delete all children and insert again)

Script 1:

class mainwindow(tk.Tk):#Initalize main windowdef __init__(self, *args, **kwargs):tk.Tk.__init__(self, *args, **kwargs)#use to shift frames....if __name__ == "__main__":...app = mainwindow()app.geometry("1920x1080")app.mainloop()

Script 2:

class Page_one(tk.Frame):def __init__(self, parent, controller):mainframe=tk.Frame.__init__(self, parent)self.controller = controller#treeview widget attached to page_onetree1=Treeviews(...)tree1.place(x=880,y=25)#Question1: How can I update the treeview widget with real-time data? 
#Question2: is there a way that I can change all treeview children simuteniously? (I dont want to delete all children and insert again)

My question is: may someone show me a structure that I could use for updating widgets in my second script.

Answer

You can modify a treeview widget with the item method:

tree.item(<item id>, text='new text', values=(...))

The item's id is returned upon item creation. And you can change all the treeview children by putting the above code in a for loop:

for item in tree.get_children(<parent>):tree.item(item, text='new text', values=(...))

tree.get_children(<parent>) returns the children of the item ('' for the root of the tree).

From your question, I understand that you have some separate process that generates data and you want to periodically update the treeview content with the new data generated. Using time.sleep would freeze the mainloop, so you should use the after tkinter method: tree.after(<time in ms>, callback, *args) will wait for the given time and then execute callback(*args). If you want a periodical callback, the trick is to call again after inside callback, e.g.:

def callback(x, y):# do something with x and y ...tree.after(2000, callback, x, y)

So once you launch callback(0, 2), it will be executed every 2s.

Here is an example with a treeview:

import tkinter as tk
from tkinter import ttkdef update_item(item):val = int(tree.item(item, 'value')[0])tree.item(item, values=(val + 1,))root.after(1000, update_item, item)def update_all_items():for item in tree.get_children():tree.item(item, values=(0,))root = tk.Tk()
tree = ttk.Treeview(root, columns=('value'))
tree.heading('value', text='Value')for i in range(5):tree.insert('', 'end', 'item%i' % i, text='item %i' % i, values=(i,))update_item('item0')tree.pack()
ttk.Button(root, text='Update', command=update_all_items).pack()root.mainloop()
https://en.xdnf.cn/q/118179.html

Related Q&A

Changing for loop to while loop

Wondering how would the following for loop be changed to while loop. While having the same output.for i in range(0,20, 4):print(i)

How to dynamically resize label in kivy without size attribute

So, I get that you can usually just use self(=)(:)texture_size (py,kv) but all of my widgets are either based on screen(root only) or size_hint. I am doing this on purpose for a cross-platform GUI. I o…

How to parallelize this nested loop in Python that calls Abaqus

I have the nested loops below. How can i parallelize the outside loop so i can distribute the outside loop into 4 simultaneous runs and wait for all 4 runs to complete before moving on with the rest of…

Comparing one column value to all columns in linux enviroment

So I have two files , one VCF that looks like88 Chr1 25 C - 3 2 1 1 88 Chr1 88 A T 7 2 1 1 88 Chr1 92 A C 16 4 1 1and another with genes that looks likeGENEI…

Can be saved into a variable one condition?

It would be possible to store the condition itself in the variable, rather than the immediate return it, when to declare it?Example:a = 3 b = 5x = (a == b) print(x)a = 5 print(x)The return isFalse Fal…

Pycharm, can not find Python version 2.7.11

I installed Python 2.7.11 on this Mac, and from terminal Python 2.7.11 can be started. However, From the interpreter of Pycharm (2016.1 version) , there is no Python 2.7.11.Any suggestions ? ThanksPS…

Python Convert Binary String to IP Address in Dotted Notation

So Im trying to read in a file with some binary strings, i.e: 10000010 00000000 0000**** ********. The script will convert the *s to both 0 and 1, so there will be two binary strings that look like thi…

Scrapy: Extracting data from source and its links

Edited question to link to original:Scrapy getting data from links within tableFrom the link https://www.tdcj.state.tx.us/death_row/dr_info/trottiewillielast.htmlI am trying to get info from the main t…

Rename file on upload to admin using Django

I have used a function in Django 1.6 to rename my files when they are uploaded through admin, but this does not work in Django 1.8. Anyone know if it is still possible to do this in 1.8?class Entry(mo…

Ignore newline character in binary file with Python?

I open my file like so :f = open("filename.ext", "rb") # ensure binary reading with bMy first line of data looks like this (when using f.readline()):\x04\x00\x00\x00\x12\x00\x00\x00…