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.


    Simple example about animating a button

    Pythonista
    2
    9
    4051
    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.
    • Phuket2
      Phuket2 last edited by

      I post this only for people learning like me. Even if it can be done a lot better we go through a learning curve.

      
      import ui
      import time
      
      '''
      	this code is not to be considered serious,
      	more the idea than anything else.
      	
      	For new guys like me learning...
      '''
      
      @ui.in_background
      def btn_font_animation(sender):
      	start = int(sender.font[1])+1
      	finish = int(start * 1.4)
      	for i in range(start,finish,2):
      		sender.font = (sender.font[0],i)
      		time.sleep(.02)
      			
      	for i in range(finish,start,-1):
      		sender.font = (sender.font[0],i)
      		time.sleep(.01)
      			
      	sender.font = (sender.font[0], start)
      	
      if __name__ == '__main__':
      	v = ui.View(name = 'Button Font Animation Test')
      	v.frame = (0,0,540,576)
      	v.background_color = 'red'
      	btn = ui.Button(title = 'Press Me')
      	btn.width, btn.height = 300,100
      	btn.x = (v.width / 2 ) - (btn.width / 2)
      	btn.y = (v.height / 2 ) - (btn.height /2) - 45
      	btn.border_width = .5
      	btn.tint_color = 'white'
      	btn.font = ('<system-bold>', 36)
      	btn.action = btn_font_animation
      	v.add_subview(btn)
      	v.present('sheet')
      
      
      1 Reply Last reply Reply Quote 0
      • Phuket2
        Phuket2 last edited by

        I could also imagine the talented graphics guys in this community opening a repo of a class of animation effects for buttons/ui elements.

        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          in general, I would caution against using sleep for animations... ui.animate was made for this purpose (you provide a function that changes a ui parameter, and a time, and the animation does the interpolation, all while keeping your ui responsive).

          however it appears that font size is not animateable, so sleep is one way to do this. all other positioning attributes, color, transforms, etc are animate able, I suspect this was an oversight...

          1 Reply Last reply Reply Quote 0
          • Phuket2
            Phuket2 last edited by

            I think if someone took the lead of designing the interface to the class, I would imagine there would be a lot of activity in a project like this.
            Maybe I am wrong, but I dont think so. My casual and inexperienced observations lead me to believe that a lot more tools/reusable code would be written for Pythonista if some really good software architects set out some frameworks for different categories that are Pythonista centric. What to call them? Extensions?, XCMD's, DLL's, plugins....it really doesn't matter. But it is a very familiar concept with operating systems and applications. Anyway, I am not a software architect, but I can write to a specification if one exists.

            1 Reply Last reply Reply Quote 0
            • Phuket2
              Phuket2 last edited by

              @JonB, yes I have been using ui.animate for sliding animations etc. I tried many things for the font animation using ui.animate, but could not get it to work (reasonably). I mean without a big fudge. I know for example I could have an object offscreen and using the values of that object to control the font size. It just does not seem right to do it that way. I also know the other advantage of using ui.amimate is its on a different thread, meaning that an animation running with ui.animate and some other task on ui_background will run simultaneously. 2 tasks running on ui_background will be run serially from my experience.

              1 Reply Last reply Reply Quote 0
              • Phuket2
                Phuket2 last edited by

                Oh, I am sure the only reason font can not be used is because it's a tuple

                1 Reply Last reply Reply Quote 0
                • JonB
                  JonB last edited by

                  you may be able to get two such animations to run by using ui.delay that calls itself, rather than in_background.

                  1 Reply Last reply Reply Quote 0
                  • Phuket2
                    Phuket2 last edited by

                    Hmm, I am pretty sure my comments about being able to use ui.animate to change a font size with a control offscreen appears to be garbage. My lack of understanding with what ui,animate is doing. I think I get it now. For some reason I thought the function passed to ui.animate was being called repeatedly. After some experimentation, it's clear it's not the case. I was sure I had seen it exhibit that behaviour, must of got mixed up with ui.background or something. Oh, live and learn. Like learning a foreign language, very embarrassing to talk to locals murdering their language, but it's the only way to learn.

                    One thing I think I discovered along the way experimenting, is that appears if a button (I only tried with a button) has an alpha of 0.0 it does not respond to clicks. However btn.on_screen reports True. Not sure if this intended or a bug. Or I am still running down the confusion road.

                    1 Reply Last reply Reply Quote 0
                    • Phuket2
                      Phuket2 last edited by

                      @JonB, yes I see a potential problem with the ui_background approach for ui elements. Keep clicking the btn multiple times, things can go a little haywire. I guess some state flags could help avoid this. I with give the ui.delay a go to do the same thing, but I would have thought it would not be fast enough to get a nice snappy font size zoom, and zoom back out.

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