Refresh text label
art last edited by
Total beginner here. I am trying to sort of animate a change to a text label, similar to how the colormixer.py example updates the hex color value. I have a loop that calls for the multiple label text changes but only the terminal value is displayed. I have tried various examples here using ui.update or ui.animate to no avail. What is the best way to do this? Thanks.
Assume a pyui with a button and a label. This is what I do not have working.
# coding: utf-8 import ui import random import time def dieRoll(): return random.randint(1, 6) def button_tapped(sender): v=sender.superview def updateLabel(): v['lblRoll'].text=str(dieRoll()) print v['lblRoll'].text for n in range(6) : updateLabel() v.set_needs_display time.sleep(1.0) #also tried #ui.delay(updateLabel,1.0) v = ui.load_view() v.present('sheet')
omz last edited by omz
ui.delayapproach is good, there's just one logic flaw in your code. You should make the delay time depend on the loop variable
n, e.g. something like
ui.delay(updateLabel, n)(or multiply
nwith some factor to make it faster/slower).
The way you've tried it (with the delay being 1 second for every call) results in all 6 updates being made at once (after 1 second), so you don't really see the individual steps. What you actually want is to do the first update after 1 second, the second after 2, etc., so it makes sense to increase the delay for every step of the animation.
art last edited by
Increasing the delay by a factor of the loop counter works great. Thanks!
I'm curious as to why
time.sleep()doesn't work. In the documentation for
time.sleep()I see this: "The actual suspension time may be less than that requested because any caught signal will terminate the sleep()". Are the ui update threads generating "signals?"
JonB last edited by
the way the ui callbacks work is that while the callback is running, you will not see any updates to the ui. So, if you have time.sleeps, the whole ui system pauses until the method exits, and you really only see the final item.
decorating the method with ui.in_background is another approach and using time.sleep.
another approach would be to use ui.animate with a duration and completion.