# Smooth seconds hand in demo clock.py browsing

• posted
0

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?

• posted
0

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.

• posted
0

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.

• posted
0

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.

• posted
0

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! ^_^

• posted
0

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. :)

• posted
0

I found some other code of mine too regarding analog clock. Here they are:

In case someone interested. :)

• posted
0

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.

• posted
0

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. :)

Internal error.

Oops! Looks like something went wrong!