Using a menu button as a clock, by constantly updating the button title
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.
blmacbeth last edited by
I did some work on a similar problem for a stopwatch. I first used the
Threadingmodule 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.delaytakes 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_delaysto call off any lingering delayed functions.
Hope this helps!
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()
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…
@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.
JonB last edited by
Make sure you use his code with both functions.... I.e
some_functioncalls itself via the delay. You would put your time update code within that function. Note you need the ability to access the textbox inside
Also Don't cancel delsys until you close your view ( such as in
will_closeof a custom view. Or better yet, inside
some_functioncheck that your view is
on_screen, and don't call delay again in that case.
@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!