使用Python的tkinter创建的模拟时钟,但一运行它,就在1秒后停止。

huangapple go评论101阅读模式
英文:

analog clock using tkinter on python but as soon as i run it it stops after 1 second

问题

  1. # imports
  2. import tkinter as cl
  3. import time
  4. import math
  5. # creating window
  6. window = cl.Tk()
  7. window.title("analog clock :3")
  8. window.geometry("400x400")
  9. # formula for how the clock
  10. """ sign(radius(t)) = x/r
  11. x = r * sign(radius(t))
  12. cos(radius(t)) = y/r
  13. y = -1 * r + cos(radius(t))"""
  14. # customising canvas
  15. canvas = cl.Canvas(width=400, height=400, bg="black")
  16. canvas.pack()
  17. # importing images
  18. bg = cl.PhotoImage(file="D:\\PythonProjects\\dial.png")
  19. canvas.create_image(200, 200, image=bg)
  20. # making the hands of the clock
  21. sec_hand_len = 90
  22. min_hand_len = 80
  23. hour_hand_len = 60
  24. center_x = 200
  25. center_y = 200
  26. sec_hand = canvas.create_line(200, 200, 200 + sec_hand_len, 200 + sec_hand_len, width=1.5, fill="red")
  27. min_hand = canvas.create_line(200, 200, 200 + min_hand_len, 200 + min_hand_len, width=2, fill="black")
  28. hour_hand = canvas.create_line(200, 200, 200 + hour_hand_len, 200 + hour_hand_len, width=4, fill="black")
  29. # defining func. and adding time
  30. def live_clock():
  31. hour = int(time.strftime("%I"))
  32. mint = int(time.strftime("%M"))
  33. sec = int(time.strftime("%S"))
  34. sec_x = sec_hand_len * math.sin(math.radians(sec * 6)) + center_x
  35. sec_y = -1 * sec_hand_len * math.cos(math.radians(sec * 6)) + center_y
  36. canvas.coords(sec_hand, center_x, center_y, sec_x, sec_y)
  37. min_x = min_hand_len * math.sin(math.radians(mint * 6)) + center_x
  38. min_y = -1 * min_hand_len * math.cos(math.radians(mint * 6)) + center_y
  39. canvas.coords(min_hand, center_x, center_y, min_x, min_y)
  40. hour_x = hour_hand_len * math.sin(math.radians(hour * 30)) + center_x
  41. hour_y = -1 * hour_hand_len * math.cos(math.radians(hour * 30)) + center_y
  42. canvas.coords(hour_hand, center_x, center_y, hour_x, hour_y)
  43. canvas.after(1000, live_clock) # Call this function again after 1 second
  44. live_clock()
  45. window.mainloop()

这是您提供的Python代码的翻译。这个代码是一个用Tkinter库创建的模拟时钟,但有一个问题,时针,分针和秒针在1秒后就不再更新。为了解决这个问题,我在live_clock函数的末尾添加了canvas.after(1000, live_clock),这将在每次调用live_clock后1秒钟后再次调用live_clock函数,以更新时钟的指针。希望这能帮助您解决问题。

英文:
  1. #imports
  2. import tkinter as cl
  3. import time
  4. import math
  5. #creating window
  6. window=cl.Tk()
  7. window.title("analog clock :3")
  8. window.geometry("400x400")
  9. #formula for how the clock
  10. """ sign(radius(t)) = x/r
  11. x = r * sign(radius(t))
  12. cos(radius(t)) = y/r
  13. y = -1 * r + cos(radius(t))"""
  14. #customising canvas
  15. canvas=cl.Canvas(width=400, height=400, bg="black")
  16. canvas.pack()
  17. #importing images
  18. bg=cl.PhotoImage(file="D:\PythonProjects\dial.png")
  19. canvas.create_image(200, 200, image=bg)
  20. #making the hands of the clock
  21. sec_hand_len = 90
  22. min_hand_len = 80
  23. hour_hand_len = 60
  24. center_x = 200
  25. center_y = 200
  26. sec_hand = canvas.create_line(200, 200, 200 + sec_hand_len, 200 + sec_hand_len, width=1.5, fill="red")
  27. min_hand = canvas.create_line(200, 200, 200 + min_hand_len, 200 + min_hand_len, width=2, fill="black")
  28. hour_hand = canvas.create_line(200, 200, 200 + hour_hand_len, 200 + hour_hand_len, width=4, fill="black")
  29. # defining func. and adding time
  30. def live_clock():
  31. hour = int(time.strftime("%I"))
  32. mint = int(time.strftime("%M"))
  33. sec= int(time.strftime("%S"))
  34. sec_x = sec_hand_len * math.sin(math.radians(sec * 6)) + center_x
  35. sec_y = -1 * sec_hand_len * math.cos(math.radians(sec * 6)) + center_y
  36. canvas.coords(sec_hand, center_x, center_y, sec_x, sec_y)
  37. min_x = min_hand_len * math.sin(math.radians(mint * 6)) + center_x
  38. min_y = -1 * min_hand_len * math.cos(math.radians(mint * 6)) + center_y
  39. canvas.coords(min_hand, center_x, center_y, min_x, min_y)
  40. hour_x = hour_hand_len * math.sin(math.radians(hour * 30)) + center_x
  41. hour_y = -1 * hour_hand_len * math.cos(math.radians(hour * 30)) + center_y
  42. canvas.coords(hour_hand, center_x, center_y, hour_x, hour_y)
  43. canvas.after(1000, live_clock)
  44. live_clock()
  45. 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 使用Python的tkinter创建的模拟时钟,但一运行它,就在1秒后停止。

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)。这就是为什么时钟只会移动一次然后停止的原因。

以下是修复后的代码:

  1. # 导入模块
  2. import tkinter as cl
  3. import time
  4. import math
  5. # 创建窗口
  6. window = cl.Tk()
  7. window.title("模拟时钟 :3")
  8. window.geometry("400x400")
  9. # 时钟的公式
  10. """
  11. sign(radius(t)) = x/r
  12. x = r * sign(radius(t))
  13. cos(radius(t)) = y/r
  14. y = -1 * r + cos(radius(t))
  15. """
  16. # 自定义画布
  17. canvas = cl.Canvas(width=400, height=400, bg="black")
  18. canvas.pack()
  19. # 导入图像
  20. bg = cl.PhotoImage(file="D:\PythonProjects\dial.png")
  21. canvas.create_image(200, 200, image=bg)
  22. # 制作时钟的指针
  23. sec_hand_len = 90
  24. min_hand_len = 80
  25. hour_hand_len = 60
  26. center_x = 200
  27. center_y = 200
  28. sec_hand = canvas.create_line(200, 200, 200 + sec_hand_len, 200 + sec_hand_len, width=1.5, fill="red")
  29. min_hand = canvas.create_line(200, 200, 200 + min_hand_len, 200 + min_hand_len, width=2, fill="black")
  30. hour_hand = canvas.create_line(200, 200, 200 + hour_hand_len, 200 + hour_hand_len, width=4, fill="black")
  31. # 定义函数和添加时间
  32. def live_clock():
  33. hour = int(time.strftime("%I"))
  34. mint = int(time.strftime("%M"))
  35. sec = int(time.strftime("%S"))
  36. sec_x = sec_hand_len * math.sin(math.radians(sec * 6)) + center_x
  37. sec_y = -1 * sec_hand_len * math.cos(math.radians(sec * 6)) + center_y
  38. canvas.coords(sec_hand, center_x, center_y, sec_x, sec_y)
  39. min_x = min_hand_len * math.sin(math.radians(mint * 6)) + center_x
  40. min_y = -1 * min_hand_len * math.cos(math.radians(mint * 6)) + center_y
  41. canvas.coords(min_hand, center_x, center_y, min_x, min_y)
  42. hour_x = hour_hand_len * math.sin(math.radians(hour * 30)) + center_x
  43. hour_y = -1 * hour_hand_len * math.cos(math.radians(hour * 30)) + center_y
  44. canvas.coords(hour_hand, center_x, center_y, hour_x, hour_y)
  45. # 1000毫秒后再次调用live_clock()函数
  46. canvas.after(1000, live_clock)
  47. # 启动时钟
  48. live_clock()
  49. # 启动GUI事件循环
  50. 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:

  1. #imports
  2. import tkinter as cl
  3. import time
  4. import math
  5. #creating window
  6. window=cl.Tk()
  7. window.title("analog clock :3")
  8. window.geometry("400x400")
  9. #formula for how the clock
  10. """ sign(radius(t)) = x/r
  11. x = r * sign(radius(t))
  12. cos(radius(t)) = y/r
  13. y = -1 * r + cos(radius(t))"""
  14. #customising canvas
  15. canvas=cl.Canvas(width=400, height=400, bg="black")
  16. canvas.pack()
  17. #importing images
  18. bg=cl.PhotoImage(file="D:\PythonProjects\dial.png")
  19. canvas.create_image(200, 200, image=bg)
  20. #making the hands of the clock
  21. sec_hand_len = 90
  22. min_hand_len = 80
  23. hour_hand_len = 60
  24. center_x = 200
  25. center_y = 200
  26. sec_hand = canvas.create_line(200, 200, 200 + sec_hand_len, 200 + sec_hand_len, width=1.5, fill="red")
  27. min_hand = canvas.create_line(200, 200, 200 + min_hand_len, 200 + min_hand_len, width=2, fill="black")
  28. hour_hand = canvas.create_line(200, 200, 200 + hour_hand_len, 200 + hour_hand_len, width=4, fill="black")
  29. # defining func. and adding time
  30. def live_clock():
  31. hour = int(time.strftime("%I"))
  32. mint = int(time.strftime("%M"))
  33. sec= int(time.strftime("%S"))
  34. sec_x = sec_hand_len * math.sin(math.radians(sec * 6)) + center_x
  35. sec_y = -1 * sec_hand_len * math.cos(math.radians(sec * 6)) + center_y
  36. canvas.coords(sec_hand, center_x, center_y, sec_x, sec_y)
  37. min_x = min_hand_len * math.sin(math.radians(mint * 6)) + center_x
  38. min_y = -1 * min_hand_len * math.cos(math.radians(mint * 6)) + center_y
  39. canvas.coords(min_hand, center_x, center_y, min_x, min_y)
  40. hour_x = hour_hand_len * math.sin(math.radians(hour * 30)) + center_x
  41. hour_y = -1 * hour_hand_len * math.cos(math.radians(hour * 30)) + center_y
  42. canvas.coords(hour_hand, center_x, center_y, hour_x, hour_y)
  43. # call the live_clock() function again after 1000 milliseconds
  44. canvas.after(1000, live_clock)
  45. # start the clock
  46. live_clock()
  47. # start the GUI event loop
  48. window.mainloop()

huangapple
  • 本文由 发表于 2023年2月14日 19:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定