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.
Smooth seconds hand in demo clock.py
-
I am new to Pythonista and was playing with the demo scripts. I was trying to figure out how to modify clock.py so that the second hand has a smooth motion rather than showing discrete seconds. Can anyone tell me what they think is the minimal change to the script to accomplish this?
-
Start your investigation by looking at the line
t = localtime()
. The time.localtime() function returns a time.struct_time which, unfortunately for your use case, only has accuracy to the second.You would therefor need get a more accurate time in milliseconds. I would probably look to datetime.timedelta but others would suggest different approaches.
Once you have obtained the current milliseconds, take a look at the line
scene.rotate((-360 / 60) * second)
. The 360 is the number of degrees in a circle so that does not need to change. The 60 is the number of seconds in a minute so you would want to change that to the number of milliseconds in a minute. Finally, you want to put the milliseconds in place of the seconds. Carefully compare your values against the(-360 / 60) * second
value to ensure that you are in the right ballpark. -
Thanks. I didn't know about datetime.timedelta. I was thinking about whether there was a way I could "artificially" create the intervening steps. In other words, each second scene is called 60 times, so move the second hand 1/60*1/60 degrees for each call.
-
I wonder if Python has duration timers that can pop - analogous to javascript's setTimeout(). You might use that instead or as well - perhaps in 100ms increments.
-
I am sure there is a better way but I used global variables to save the second count and the number of times that the scene function was called (j) to modify the scene.rotate((-360 / 60) * second - j/10). Not pretty, but it basically works.
-
I made another analog clock a while ago, just for exercising. But instead of using transformation, like in the example, I draw the clock using simple math (trigonometry). Let me find the code and post it on pastebin.
I'll be back! ^_^
-
Here we go... Check it out at <strike>http://pastebin.com/vAc3SyVF</strike>
Update: Sorry, the link above is deleted and no longer valid. Use the new one below. And the forum can't display the strike attribute correctly. :)
Hope it useful. :)
-
I found some other code of mine too regarding analog clock. Here they are:
- regular clock: http://pastebin.com/pCUuWfSW
- radial clock: http://pastebin.com/M165BTzE
- racing clock: http://pastebin.com/BnJrKY4v
In case someone interested. :)
-
Thanks very much for posting these. My approach was problematic in that it depended on Scene being called exactly 60 times/sec. What I found was that scene was being called anywhere from 35-60 times probably depending on how busy the iPad or iPad-mini was at the time.
-
It will be called exactly 60 times per second if you got very very simple things to draw which require less than 1/60 second to process. But once you got a bit more complex drawing and or computation, the process would take longer. The more complex your program the longer it takes to draw. So, you need another way to get your program syncs with the real time. :)