简体   繁体   中英

How to use tkinter Canvas to create multiple text one time?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM