如何使用 tkinter Canvas 一次创建多个文本?
How to use tkinter Canvas to create multiple text one time?
例如:
for i in range(10000):
canvas.create_text(10*i,100,text='test',fill='red')
主要window卡在运行这部分。如何在加载文本时避免它?
你可以使用after()
来替换for循环,这样它就不会阻塞tkinter mainloop
:
def show_text(n=0):
# show 20 text in each iteration
for i in range(20):
y, x = divmod(n+i, 20)
canvas.create_text(x*50, y*10, text='test', fill='red', anchor='nw')
n += 20
if n < 10000:
canvas.after(1, show_text, n)
show_text() # start the after() loop
例如:
for i in range(10000):
canvas.create_text(10*i,100,text='test',fill='red')
主要window卡在运行这部分。如何在加载文本时避免它?
你可以使用after()
来替换for循环,这样它就不会阻塞tkinter mainloop
:
def show_text(n=0):
# show 20 text in each iteration
for i in range(20):
y, x = divmod(n+i, 20)
canvas.create_text(x*50, y*10, text='test', fill='red', anchor='nw')
n += 20
if n < 10000:
canvas.after(1, show_text, n)
show_text() # start the after() loop