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.


    Understanding UI redraw / animation

    Pythonista
    2
    3
    2213
    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.
    • spinmaster
      spinmaster last edited by

      Hi All.

      I'm loving the new 2.0 update.

      I'm trying to get my head around how the UI module handles redraws / screen updates.

      I'm trying to do a simple test where a series of labels blink in series (kind of like a row of LEDs, but text labels instead). I've found the UI.animate and UI.delay functions . However, I get an error when placing UI.delay in a loop.

      It's calls the function correctly the first call, but on subsequent it gives me error of "Type Error: Expected Callable Function"

      Here is code...can anyone illuminate problem?

      import ui
      
      def colorIt(lbl):
      	lbl.background_color='#ff0000'
      
      def buttonClick(sender):
      	for item in llst:
      		print item
      		ui.delay(colorIt(item),1.0)
      	
      v = ui.load_view()
      
      lbl1=v['label1']
      lbl2=v['label2']
      lbl3=v['label3']
      llst=[lbl1,lbl2,lbl3]
      
      v.present('sheet')
      
      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by omz

        ui.delay expects a callable object, typically a function, but you're passing the result of a function call (which is None in this case because colorIt doesn't return anything).

        The function that you pass to ui.delay() must not take any parameters. To get the result you're looking for, you can use functools.partial() to transform a function with arguments (colorIt()) into one without.

        This should work (untested):

        import ui
        from functools import partial
        
        def colorIt(lbl):
            lbl.background_color='#ff0000'
        
        def buttonClick(sender):
            for i, item in enumerate(llst):
                print item
                ui.delay(partial(colorIt, item), 1.0 * (i+1))
            
        v = ui.load_view()
        
        lbl1=v['label1']
        lbl2=v['label2']
        lbl3=v['label3']
        llst=[lbl1,lbl2,lbl3]
        
        v.present('sheet')
        
        spinmaster 1 Reply Last reply Reply Quote 0
        • spinmaster
          spinmaster @omz last edited by spinmaster

          @omz said:

          ui.delay expects a callable object, typically a function, but you're passing the result of a function call (which is None in this case because colorIt doesn't return anything).

          Many thanks OMZ. I understand.

          I suppose to other approach is to refactor the code so that the called function has no parameters - but for this simple example partial seems easier.

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