omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    3
    4
    2654
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • art
      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')	
      
      1 Reply Last reply Reply Quote 2
      • omz
        omz last edited by omz

        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 variable n, e.g. something like ui.delay(updateLabel, n) (or multiply n 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.

        1 Reply Last reply Reply Quote 1
        • art
          art last edited by

          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 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?"

          1 Reply Last reply Reply Quote 0
          • JonB
            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.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors