英文:
analog clock using tkinter on python but as soon as i run it it stops after 1 second
问题
# 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) # Call this function again after 1 second
live_clock()
window.mainloop()
这是您提供的Python代码的翻译。这个代码是一个用Tkinter库创建的模拟时钟,但有一个问题,时针,分针和秒针在1秒后就不再更新。为了解决这个问题,我在live_clock
函数的末尾添加了canvas.after(1000, live_clock)
,这将在每次调用live_clock
后1秒钟后再次调用live_clock
函数,以更新时钟的指针。希望这能帮助您解决问题。
英文:
#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()
thats my code but as soon as i run it it stopped after 1 second
i tried making a clock but it stopped working after 1 second
keep in mind i am super super new and i would really appreciate any help since this is my project for 11th grade cs
i had issues with the code before this but i managed to figure them out and fix them on my own but i cannot seem to figure out how to fix this.
as soon as i run the code the tkinter window open and the second hand moves once but it stops and doesnt move again, i thought maybe the seconds hand had the problem but i waited a minute and the minute hand didnt move either so idk what to do now please help me out.
答案1
得分: 1
提供的代码存在一个问题,即函数live_clock()只在延迟1000毫秒后被调用一次,使用canvas.after(1000, live_clock)。这就是为什么时钟只会移动一次然后停止的原因。
以下是修复后的代码:
# 导入模块
import tkinter as cl
import time
import math
# 创建窗口
window = cl.Tk()
window.title("模拟时钟 :3")
window.geometry("400x400")
# 时钟的公式
"""
sign(radius(t)) = x/r
x = r * sign(radius(t))
cos(radius(t)) = y/r
y = -1 * r + cos(radius(t))
"""
# 自定义画布
canvas = cl.Canvas(width=400, height=400, bg="black")
canvas.pack()
# 导入图像
bg = cl.PhotoImage(file="D:\PythonProjects\dial.png")
canvas.create_image(200, 200, image=bg)
# 制作时钟的指针
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")
# 定义函数和添加时间
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)
# 1000毫秒后再次调用live_clock()函数
canvas.after(1000, live_clock)
# 启动时钟
live_clock()
# 启动GUI事件循环
window.mainloop()
请注意,我已将HTML编码字符“"”更正为正常的双引号(")以使代码更容易阅读。
英文:
The issue with the provided code is that the function live_clock() is called only once after a delay of 1000 milliseconds using canvas.after(1000, live_clock). This is why the clock moves only once and then stops.
This might work:
#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)
# call the live_clock() function again after 1000 milliseconds
canvas.after(1000, live_clock)
# start the clock
live_clock()
# start the GUI event loop
window.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论