Python 3.x Tkinter动画-模拟模拟时钟

Python 3.x Tkinter动画-模拟模拟时钟,python-3.x,tkinter,Python 3.x,Tkinter,我目前已经用Tkinter制作了时钟和时、分、秒针 hour_num=[3,2,1,12,11,10,9,8,7,6,5,4] 对于以小时为单位的i_num: text_x=原点[0]+时钟半径*数学cos(θ) text_y=原点[1]-时钟半径*math.sin(θ) θ+=d_θ 屏幕。创建文本(文本x,文本y,文本=i,font=“Arial 25”,fill=“white”) ##时间信息 hour=datetime.now().hour 分钟=datetime.now().minut

我目前已经用Tkinter制作了时钟和时、分、秒针

hour_num=[3,2,1,12,11,10,9,8,7,6,5,4]
对于以小时为单位的i_num:
text_x=原点[0]+时钟半径*数学cos(θ)
text_y=原点[1]-时钟半径*math.sin(θ)
θ+=d_θ
屏幕。创建文本(文本x,文本y,文本=i,font=“Arial 25”,fill=“white”)
##时间信息
hour=datetime.now().hour
分钟=datetime.now().minute
second=datetime.now().second
二手=屏幕。创建线(原点[0],原点[1],原点[0],原点[1]-时钟半径+50,宽度=13,填充=“蓝色”)
分钟指针=屏幕。创建线(原点[0],原点[1],原点[0],原点[1]-时钟半径+70,宽度=13,填充=“绿色”)
hourhand=屏幕。创建线(原点[0],原点[1],原点[0],原点[1]-时钟半径+90,宽度=13,填充=“红色”)
现在画布看起来是这样的:

有人能帮我用当前时间制作时钟指针的动画吗

我试着用三角学来计算每小时之间的距离(取与原点夹角的余弦比)。首先,我意识到它不是完美的直角三角形,其次。时针将不现实:2:50的时针将更接近3而不是2


谢谢

这是我几年前写的一个时钟的代码片段

def getHMS():
    time = datetime.now()
    h,m,s = time.hour, time.minute, time.second
    return h,m,s

def updateClock():
    h,m,s = getHMS()
    ac = -90    # Correction angle since 0degrees should be at the top
    ha = (((h%12)+(m/60))/12)*360 + ac
    ma = ((m + (s/60)) / 60 )*360 + ac
    sa = ((s / 60) * 360) + ac
    moveHand(hour_hand, ha)
    moveHand(minute_hand, ma)
    moveHand(second_hand, sa)
    canvas.after(1000,updateClock)
在UpdateLock方法中,请注意,为了计算出时针的角度,我同时使用了小时
h
和分钟
m
值。与分针类似,我使用分和秒值来设置角度

编辑:一些关于画手的额外帮助

首先,你需要画一只手(不管画在哪里)

然后,您需要根据当前时间使用我们计算的角度移动该手

def moveHourHand(angle):
    hour_length = 150
    #Centre position of hand
    x1 = 250 
    y1 = 250
    #End Postition of hand
    x2 = x1 + hour_length * math.cos(math.radians(angle))
    y2 = y1 + hour_length * math.sin(math.radians(angle))
    #Move existing line to new position
    canvas.coords(hour_hand_line, x1,y1,x2,y2)

开始位置
x1
y1
是时钟的中心,测线
x2
y2
的结束位置基于测线的长度,并根据三角学进行一些调整。然后我们把线移到那个位置。

你可以用小时和分钟来计算时针的角度。你能给我更多关于moveHand()函数的提示吗?如何调整坐标使其与角度匹配?@ENG2D提供了一些其他帮助。你需要做一些工作才能把所有的事情都做好,但是复杂的部分是你可以使用的。非常感谢,我没想到会有一个完整的答案,一个提示就足够了!非常感谢你!
def moveHourHand(angle):
    hour_length = 150
    #Centre position of hand
    x1 = 250 
    y1 = 250
    #End Postition of hand
    x2 = x1 + hour_length * math.cos(math.radians(angle))
    y2 = y1 + hour_length * math.sin(math.radians(angle))
    #Move existing line to new position
    canvas.coords(hour_hand_line, x1,y1,x2,y2)