子窗口中的小部件使用 tkinter python 中的实时数据进行更新 [英] widget in sub-window update with real-time data in tkinter python

查看:159
本文介绍了子窗口中的小部件使用 tkinter python 中的实时数据进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 after/time.sleep 来更新树视图,但它不适用于主循环.

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

我的问题是:

  • 如何使用实时数据更新树视图小部件?
  • 有没有一种方法可以同时更改所有树视图子项?(我不想删除所有孩子并再次插入)

脚本 1:

class mainwindow(tk.Tk):
    #Initalize main window
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

       #use to shift frames
       ....
 if __name__ == "__main__":
    ...
    app = mainwindow()
    app.geometry("1920x1080")
    app.mainloop()

脚本 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_one
        tree1=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.

推荐答案

您可以使用 item 方法修改树视图小部件:

You can modify a treeview widget with the item method:

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

在创建项目时返回项目的 id.您可以通过将上述代码放入 for 循环来更改所有树视图子项:

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() 返回项的子项('' 表示树的根).

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

根据您的问题,我了解到您有一些单独的过程来生成数据,并且您希望使用生成的新数据定期更新树视图内容.使用 time.sleep 会冻结主循环,所以你应该使用 after tkinter 方法: tree.after( 将等待给定的时间,然后执行 callback(*args).如果你想要一个周期性的回调,诀窍是在 callback 中再次调用 after,例如:

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)

所以一旦你启动callback(0, 2),它将每2s执行一次.

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 ttk

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

这篇关于子窗口中的小部件使用 tkinter python 中的实时数据进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆