Question

Goal: push a button to show and change the background of elements from a list one by one with a delay of n seconds until the entire text is highlighted.

I wrapped:

text2.insert(INSERT,sonsilabo,"silabo")
text2.pack()
time.sleep(2)

in a for-loop to do that for each element.

But the program still wait for the entire cycle to finish and show the final result.

from Tkinter import *
import Tkinter
import time

root = Tk()

text2 = Text(root)
text2.tag_config("silabo",background="green")

teksto = ['Sa','lu','to','n',' ','mi',' ','no','mi','ĝa','s',' ','A','b','de','l']

def kolorigu():
    text2.delete('1.0', END)
    for sonsilabo in teksto:
        text2.insert(INSERT,sonsilabo,"silabo")
        text2.pack()
        time.sleep(2)

text2.pack()
B = Button(root, text ="Kolorigu", command = kolorigu)
B.pack()

root.mainloop()

Any idea?

Was it helpful?

Solution

After you add some text, you need to update the textbox by calling text2.update_idletasks:

def kolorigu():
    text2.delete('1.0', END)
    for sonsilabo in teksto:
        text2.insert(INSERT,sonsilabo,"silabo")
        ########################
        text2.update_idletasks()
        ########################
        time.sleep(2)

Also, I removed the text2.pack() line inside kolorigu because it is unnecessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top