简体   繁体   English

Tkinter文本高亮显示延迟不起作用

[英]Tkinter text highlight with delay doesn't work

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. 目标:按下按钮以逐个显示和更改列表中元素的背景,延迟n秒,直到高亮显示整个文本。

I wrapped: 我包裹:

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

in a for-loop to do that for each element. 在for循环中为每个元素执行此操作。

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? 任何想法?

After you add some text, you need to update the textbox by calling text2.update_idletasks : 添加一些文本之后,您需要通过调用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. 另外,我除去text2.pack()内部线kolorigu因为它是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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