这个模拟时钟中的这部分代码到底是做什么来移动时钟指针的?

2024-10-04 11:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我不确定这类问题是否在这里得到了回答,但不管怎样,还是在这里

# moving hour hand
def moveHourHand():
   currentHourInternal = datetime.datetime.now().hour
   degree = (currentHourInternal - 15) * -30
   currentMinuteInternal = datetime.datetime.now().minute
   degree = degree + -0.5 * currentMinuteInternal
   hourHand.setheading(degree)
   wn.ontimer(moveHourHand, 60000)


# moving minute hand
def moveMinuteHand():
    currentMinuteInternal = datetime.datetime.now().minute
    degree = (currentMinuteInternal - 15) * -6
    currentSecondInternal = datetime.datetime.now().second
    degree = degree + (-currentSecondInternal * 0.1)
    minuteHand.setheading(degree)
    wn.ontimer(moveMinuteHand, 1000)

# moving second hand
def moveSecondHand():
    currentSecondInternal = datetime.datetime.now().second
    degree = (currentSecondInternal - 15) * -6
    secondHand.setheading(degree)
    wn.ontimer(moveSecondHand, 1000)

这是turtle在python中构建的模拟时钟的一部分。(不是我的。This is the source)这部分是为手的运动创造功能

我正在创造一个我自己的时钟,我正在寻找不同的方法让指针移动。这一个似乎工作得最好,但我真的不明白到底发生了什么。我知道它从datetime库中获取了一个值,但之后的一切都令人困惑。尤其是带有“度”的部分。“15”来自哪里

如果有人能以我能理解的方式解释,我将不胜感激


Tags: datetimedefnowsecondhandmovinghourwn
1条回答
网友
1楼 · 发布于 2024-10-04 11:26:34
# moving hour hand
def moveHourHand():
   currentHourInternal = datetime.datetime.now().hour #Gets current local hour
   degree = (currentHourInternal - 15) * -30 #Hour degree, look below
   currentMinuteInternal = datetime.datetime.now().minute #Gets minutes for moving the hour hand between hours 
   degree = degree + -0.5 * currentMinuteInternal #Minute degree, also, look below
   hourHand.setheading(degree) #This might send the degree to the function that move the hand
   wn.ontimer(moveHourHand, 60000) #Im not sure about this one but i think that tells that function what and when to be called again, in milli seconds (60000ms = 60s = 1m)

小时学位:

-在圆中,通常0度是最右点,但在时钟中,0度是顶点,如图所示。以证明15人被起诉。每小时之间有30度(360度圆/12小时)。它是负的,因为增加正的度数是逆时针的

circle with degreesclock

e.g. (hour - 15) * -30
12 hours -> (12 - 15) * -30 = 90 degrees. Look the images, 90 degrees corresponds to 12h
3 hours -> (3 - 15) * -30 = 360 degrees
6 hours -> (6 - 15) * -30 = 270 degrees
9 hours -> (9 - 15) * -30 = 180 degrees

分钟学位:

工作原理与小时类似,乘以-0.5,因为每小时有30度和60分钟,计算一下,(30度/60分钟)每分钟1/2度。和以前一样,这是因为正度数与时钟方向相反,所以负度数与时钟方向相反。然后将其添加到总度数中,以便在数字之间移动时针

e.g. (minutes * -0.5)
0 minutes:  0 degrees
15 minutes: -7.5 degrees
30 minutes: -15 degrees
45 minutes: -22.5 degrees

相关问题 更多 >