Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Refresh text label
-
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')
-
The
ui.delay
approach is good, there's just one logic flaw in your code. You should make the delay time depend on the loop variablen
, e.g. something likeui.delay(updateLabel, n)
(or multiplyn
with 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.
-
Increasing the delay by a factor of the loop counter works great. Thanks!
ui.delay(updateLabel,0.5*n)
I'm curious as to why
time.sleep()
doesn't work. In the documentation fortime.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?" -
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.