For example, there is a code:

def tk_sleep(): status_send = Tk() status_send.geometry('400x340') tx_1 = Text(status_send, font=('times', 12), width=62, height=15, wrap=WORD) tx_1.pack() for i in range(10): tx_1.insert(1.0, 'hello world') tx_1.update() sleep(5) 

At the same time freezes the whole window with the widget. How to avoid it?

1 answer 1

Long sleep and causes the interface to hang in tkinter . If you need to do something with a periodicity of a few seconds, then it is better to do it after :

 from tkinter import * def tk_sleep(): def do_something(i): if i <= 0: return else: tx_1.insert(1.0, 'hello world\n') # "Планируем" выполнение функции через 1 секунду: status_send.after(1000, do_something, i-1) status_send = Tk() status_send.geometry('400x340') tx_1 = Text(status_send, font=('times', 12), width=62, height=15, wrap=WORD) tx_1.pack() do_something(10) mainloop() tk_sleep() 
  • @jfs, yes, thank you, did not know about this feature. - insolor