Home > Back-end >  How to use tkinter Canvas to create multiple text one time?
How to use tkinter Canvas to create multiple text one time?

Time:12-31

For example:

for i in range(10000):
   canvas.create_text(10*i,100,text='test',fill='red')

The main window get stuck when run this part. how can I avoid it when loading the text?

CodePudding user response:

You can use after() to replace the for loop so that it won't block the 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
  • Related