python tkinter treeview停止更新到屏幕

问题描述 投票:0回答:1

我想找到一个可以用滚动条控制树形视图的函数显示屏幕更新还是不显示。

由于我经常更改树视图的内容会导致效率下降,所以我只想让树视图当我需要时。

我做过一些研究,说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()
python tkinter treeview
1个回答
0
投票

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

您正在使用的.after非常好,这意味着您可以使用.after_cancel停止循环。

代码需要重组,但是我将让您自己进行重组,在您已经定义的functions中,methods会比class更好。

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()
© www.soinside.com 2019 - 2024. All rights reserved.