python tkinter treeview停止更新到屏幕

2024-05-19 15:05:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找到一个功能,可以控制与滚动条树视图 是否显示更新到屏幕

因为我经常更改treeview内容导致效率下降,所以我只想让treeview 当我需要的时候

我做了一些研究,说tkinter只在事件触发时更新 ,但我使用线程更改Threeview内容也会导致屏幕更新

from tkinter.ttk  import *
from tkinter import *

root = Tk()
root.geometry('800x800')
root.bind('<Escape>',  lambda x:root.destroy() )

class Run_item_Tree(Frame):
    def __init__(self, roots):
        Frame.__init__(self, roots)
        t = Treeview(self, selectmode=NONE, height=10)
        self.t = t
        t["columns"] = ("1", "2")
        t['show'] = 'headings'
        t.column("1", width=100, anchor='c')
        t.column("2", width=50, anchor='c')

        t.heading("1", text="Title")
        t.heading("2", text="UL")

        t.pack(fill =BOTH,side =LEFT)

        vsb = Scrollbar(self, orient="vertical", command=t.yview)
        vsb.pack(fill = Y,side = RIGHT)
        self.vsb = vsb
        t.configure(yscrollcommand=vsb.set)
        self.pack()
a = Run_item_Tree(root)
item_num = 4000
for i in range(item_num):
    a.t.insert('',index=END,text = i,values = (i,i+1,i+2,i+4),tags=('pass',))
count = 0
def update_test():
    global  count
    #a.t.yview_moveto(count/item_num)
    a.t.item(a.t.get_children()[0],values = (count,count+1,count+2,count+4) )
    count= (count+1)%item_num
    root.after(100, update_test)

btn = Button(root,text='update/unupdate',command=lambda  : SomeFunction())
btn.pack()

root.after(100,update_test)
root.mainloop()

Tags: textfromtestself内容屏幕tkintercount
1条回答
网友
1楼 · 发布于 2024-05-19 15:05:40

要控制Treeview更新,需要有一个单独的函数来处理update_test函数是否被调用

您正在使用.after,这很好,这意味着您可以使用.after_cancel来停止循环

代码需要重新构造,但我将让您自己进行重新构造,functions会更好,因为您已经定义了class中的methods

from tkinter.ttk  import *
from tkinter import *

root = Tk()
root.geometry('800x800')
root.bind('<Escape>',  lambda x:root.destroy() )

class Run_item_Tree(Frame):
    def __init__(self, roots):
        Frame.__init__(self, roots)
        t = Treeview(self, selectmode=NONE, height=10)
        self.t = t
        t["columns"] = ("1", "2")
        t['show'] = 'headings'
        t.column("1", width=100, anchor='c')
        t.column("2", width=50, anchor='c')

        t.heading("1", text="Title")
        t.heading("2", text="UL")

        t.pack(fill =BOTH,side =LEFT)

        vsb = Scrollbar(self, orient="vertical", command=t.yview)
        vsb.pack(fill = Y,side = RIGHT)
        self.vsb = vsb
        t.configure(yscrollcommand=vsb.set)
        self.pack()

a = Run_item_Tree(root)
item_num = 4000
for i in range(item_num):
    a.t.insert('',index=END,text = i,values = (i,i+1,i+2,i+4),tags=('pass',))
count = 0
update = True

def call_update():
    global update, update_after
    if update:
        update = False
        update_test()
    else:
        update_after_cancel = root.after_cancel(update_after)
        update = True


def update_test():
    global  count, update, update_after
    #a.t.yview_moveto(count/item_num)
    a.t.item(a.t.get_children()[0],values = (count,count+1,count+2,count+4) )
    count= (count+1)%item_num
    update_after = root.after(100, update_test)

btn = Button(root,text='update/unupdate',command=lambda  : call_update())
btn.pack()

#root.after(100,update_test)
root.mainloop()

相关问题 更多 >