#imports
import tkinter as cl
import time
import math
#creating window
window=cl.Tk()
window.title("analog clock :3")
window.geometry("400x400")
#formula for how the clock
""" sign(radius(t)) = x/r
x = r * sign(radius(t))
cos(radius(t)) = y/r
y = -1 * r + cos(radius(t))"""
#customising canvas
canvas=cl.Canvas(width=400, height=400, bg="black")
canvas.pack()
#importing images
bg=cl.PhotoImage(file="D:\PythonProjects\dial.png")
canvas.create_image(200, 200, image=bg)
#making the hands of the clock
sec_hand_len = 90
min_hand_len = 80
hour_hand_len = 60
center_x = 200
center_y = 200
sec_hand = canvas.create_line(200, 200, 200 + sec_hand_len, 200 + sec_hand_len, width=1.5, fill="red")
min_hand = canvas.create_line(200, 200, 200 + min_hand_len, 200 + min_hand_len, width=2, fill="black")
hour_hand = canvas.create_line(200, 200, 200 + hour_hand_len, 200 + hour_hand_len, width=4, fill="black")
# defining func. and adding time
def live_clock():
hour = int(time.strftime("%I"))
mint = int(time.strftime("%M"))
sec= int(time.strftime("%S"))
sec_x = sec_hand_len * math.sin(math.radians(sec * 6)) + center_x
sec_y = -1 * sec_hand_len * math.cos(math.radians(sec * 6)) + center_y
canvas.coords(sec_hand, center_x, center_y, sec_x, sec_y)
min_x = min_hand_len * math.sin(math.radians(mint * 6)) + center_x
min_y = -1 * min_hand_len * math.cos(math.radians(mint * 6)) + center_y
canvas.coords(min_hand, center_x, center_y, min_x, min_y)
hour_x = hour_hand_len * math.sin(math.radians(hour * 30)) + center_x
hour_y = -1 * hour_hand_len * math.cos(math.radians(hour * 30)) + center_y
canvas.coords(hour_hand, center_x, center_y, hour_x, hour_y)
canvas.after(1000, live_clock)
live_clock()
window.mainloop()
这是我的代码,但只要我运行它,它停止后1秒钟
我试着做一个时钟,但它停止工作后1秒钟,请记住,我是超级超级新的,我真的会很感激任何帮助,因为这是我的项目为11年级的cs:)
在此之前,我的代码有问题,但我设法解决了这些问题,并自行修复了它们,但我似乎找不出如何修复这个问题。只要我运行代码,Tkinter窗口就会打开,秒针移动一次,但它会停止,不再移动,我想可能是秒针出了问题,但是我等了一分钟,分针也不动了,所以我不知道现在该怎么办,请帮我解决。
1条答案
按热度按时间nwlls2ji1#
所提供代码的问题在于,使用canvas. after(1000,live_clock)时,函数live_clock()仅在延迟1000毫秒后调用一次,这就是时钟仅移动一次然后停止的原因。
这可能行得通: