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.


    Using a menu button as a clock, by constantly updating the button title

    Pythonista
    4
    6
    3442
    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 just wanted to add the time to a button on the menu bar(constantly updating using the title). The code below works, but the refreshing is very bad. I am guessing I am mis-using the menu buttons intended function here. Look, it's not important. I just thought, I am possibly missing something simple also about refreshing. Will understand if I get no replies.

      
      	@ui.in_background
      	def time_btn(self):
      		#self.menu_button(...) just adding a button to 
      		#aView.right_button_items, returning the button.
      		
      		btn = self.menu_button('Time', False,None)
      		#btn.enabled = False 
      		while(True):
      			if not self.view.on_screen: break
      			btn.title = time.strftime('%H:%M:%S')
      			time.sleep(1)
      
      

      As a side question, I also have Editorial, I wrote this text up in Editorial, and I could not see an easy way to copy and paste the markdown directly into here. I have only just began to use Editorial and markdown, but it makes sense for me to write posts like this in Editorial so I can see the formatted text and paste it in here directly.

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

        I did some work on a similar problem for a stopwatch. I first used the Threading module to create a separate thread that constantly updated the time label. I found this to work fairly well; I'll post the code if I can figure out where it went…

        Another approach that works much better and does not require the use of any extra modules is ui.delay. @omz has talked about about this before on the forums, I will find the link later and update my comment, unless some other kind person finds it before me.

        ui.delay takes in a function and an amount of time. It runs that function after the amount of time you set. When your program completes, you can set up the will_close(self) function to call ui.cancel_delays to call off any lingering delayed functions.

        Hope this helps!

        B

        EDIT: because I was on my iPhone when I originally wrote this and I made a mistake...

        I originally used this for a stopwatch application to learn how to create a speedometer application.

        def some_function():
            ## do stuff here...
            ui.delay(some_function, .5) ## half-second delay
        
        def button_pressed(sender):
            ## do whatever...
            ui.delay(some_function, .5) ## half-second delay
        
        ## don't forget to cancel your delays...
        ui.cancel_delays()
        
        
        1 Reply Last reply Reply Quote 0
        • Gerzer
          Gerzer last edited by

          Why do you need the time.sleep(1)? Even if the button's title is updated every 10th of a second, the time itself won't change, and updating a button title shouldn't compromise performance. I don't think, at least…

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

            @Gerzer, I did time.sleep(1) because no reason to update it more than once a second. Just thinking about wasted cpu cycles. If I had milliseconds display , then would attempt to redraw more often. I hate tight loops. But my experiment.

            @blmacbeth, I tried your solution, it did not work correctly. The title of my menu button was only updated once. But I am not saying your solution is wrong, something about my understanding of the language/code is wrong. Will do more tomorrow.

            Thank you

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

              Make sure you use his code with both functions.... I.e some_function calls itself via the delay. You would put your time update code within that function. Note you need the ability to access the textbox inside some_function...

              Also Don't cancel delsys until you close your view ( such as in will_close of a custom view. Or better yet, inside some_function check that your view is on_screen, and don't call delay again in that case.

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

                @JonB, thanks. Hmmm, you guessed right. I wasn't calling ui.delay() in the function. I had the ui.cancel_delays() correct, Test view_on_screen before doing anything! If not on screen, call ui.cancel_delays() and return.

                The new approach still flickers on updating the button in the title. I checked it on a normal button on the sheet, same effect. A lot of flickering, I know a label works just fine, was focusing on a button because that's what I can put in the menu bar. I know there other ways around this by hiding the menu bar and drawing my own. Or possibly creating images on the fly of the time and drawing it myself. Doing these things to learn about the ui module. May never need to use the time in a button on the menu, but learning a lot just by trying different ideas. Thanks again to all for comments, really does help.

                Ps. I wish Apple would implement @omz cursor navigation into their keyboard!

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