import tkinter as tk
import time
class Example(tk.Toplevel):
def __init__(self, root):
tk.Toplevel.__init__(self, root)
self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
self.frame = tk.Frame(self.canvas, background="#ffffff")
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas.create_window((4,4), window=self.frame, anchor="nw",
tags="self.frame")
self.frame.bind("<Configure>", self.onFrameConfigure)
self.populate()
self.l= tk.Label(self.frame, text= 1)
self.l.grid()
self.cb= tk.Button(self.frame, text= "asdf", command= self.cnt)
self.cb.grid()
def cnt(self):
slowlist= []
slow= 0
for x in range(10000):
now= time.time()
self.l["text"]+=1
end= time.time()
if end-now >= 0.001:
slow+=1
slowlist.append(end-now)
print (slow)
print (slowlist[:5])
def populate(self):
'''Put in some fake data'''
for row in range(100):
tk.Label(self.frame, text="%s" % row, width=3, borderwidth="1",
relief="solid").grid(row=row, column=0)
t="this is the second column for row %s" %row
tk.Label(self.frame, text=t).grid(row=row, column=1)
def onFrameConfigure(self, event):
'''Reset the scroll region to encompass the inner frame'''
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
if __name__ == "__main__":
root=tk.Tk()
Example(root)
root.mainloop()
Above is my code, a large part of the code comes from this post. And I am concerned when I repeatedly update the label text by using self.l["text"]+=1, there would be rare cases (about 70 instances out of 10000 updates) that it takes around 0.11 sec to update the label text, while the remaining iterations act a lot faster.
I wonder a) why would there be cases where updating become considerably slower? especially when it seems kind of constant to have about 70 slow cases in every iteration of 10000, I wonder if there is any reason for that "constant" time drift. and b) how to avoid those slow instances when running a large iteration loop that requires to update the label text quite often?