Reputation: 509
I have to stop a loop and resume after many seconds. I tried to use after(), but the loop don't freeze. And when I use time.sleep() (works out tkinter), tkinter freeze. Have another way or a function equivalent time.sleep() without freeze.
code:
for message in listOfMessages:
time.sleep(message.time)
#manipulate message
#change text widget
This will freeze my tkinter app.
I tried to use after() but message.time is a float: 0.5054564 to 1.5234244. It's random. And after supports only int. I can't convert to int, because low numbers make differents. And if i convert 0.52342342 in int: 523... i lost the others 42342
and this dont work too:
def changes(#values):
#manipulate message
#change text widget
for message in listOfMessages:
app.after(message.time, lambda: changes(#values))
Have another function equivalent time.sleep thats not freeze tkinter and is different that after()? If not, have another way to do this? Thanks for attention.
Upvotes: 2
Views: 2186
Reputation: 414825
To create an analog of:
for message in listOfMessages:
time.sleep(message.time)
change(message)
in tkinter
:
def update_widget(app, messages):
message = next(messages, None)
if message is None: # end of the loop
return
delay = int(message.time * 1000) # milliseconds
app.after(delay, change, message) # execute body
app.after(delay, update_widget, app, messages) # next iteration
update_widget(app, iter(listOfMessages))
If you want to wait until change(message)
finishes before continuing the loop:
def iterate(message, app, messages):
change(message)
update_widget(app, messages)
def update_widget(app, messages):
message = next(messages, None)
if message is None: # end of the loop
return
delay = int(message.time * 1000) # milliseconds
app.after(delay, iterate, message, app, messages)
update_widget(app, iter(listOfMessages))
Upvotes: 3