from tkinter import Tk, Canvas, Button
import tkinter as tk
import math
import time
is_paused = False
paused_remaining_seconds = 0
def update_clock(canvas, minute_angle, second_angle, minute_hand, second_hand):
x_intersection = 400
y_intersection = 400
x_minute = 300 * math.cos(math.radians(-minute_angle))
y_minute = 300 * math.sin(math.radians(-minute_angle))
canvas.coords(minute_hand, x_intersection - 0.1 * x_minute, y_intersection + 0.1 * y_minute, x_intersection + x_minute, y_intersection - y_minute)
x_second = 315 * math.cos(math.radians(-second_angle))
y_second = 315 * math.sin(math.radians(-second_angle))
canvas.coords(second_hand, x_intersection - 0.3 * x_second, y_intersection + 0.3 * y_second, x_intersection + x_second, y_intersection - y_second)
def draw_sawtooth_circle(canvas, x, y, r, num_segments, color, second_angle):
for i in range(num_segments):
angle1 = i * 2 * math.pi / num_segments
angle2 = (i + 0.5) * 2 * math.pi / num_segments
x1 = x + (r + 20) * math.cos(angle1)
y1 = y + (r + 20) * math.sin(angle1)
x2 = x + r * math.cos(angle2)
y2 = y + r * math.sin(angle2)
if second_angle < 0:
second_angle = 360 + second_angle
if math.fabs(angle1 * (180 / math.pi) - second_angle) < 30:
canvas.create_line(x1, y1, x2, y2, fill=color, width=6)
else:
canvas.create_line(x1, y1, x2, y2, fill="orange", width=6)
def countdown(total_seconds, root):
global is_paused
global paused_remaining_seconds
if total_seconds <= 60:
root.title("倒计时" + str(total_seconds) + "秒")
else:
root.title("倒计时" + str(total_seconds // 60) + "分钟")
start_time = time.time()
minute_angle = 0
second_angle = 0
canvas.delete("time_END")
elapsed_time = time.time() - start_time
remaining_seconds = total_seconds - elapsed_time
while True:
if not is_paused:
elapsed_time = time.time() - start_time
remaining_seconds = total_seconds - elapsed_time
minute = int(remaining_seconds / 60)
second = int(remaining_seconds % 60)
second_angle = second * 6 - 90
if second!= 0:
minute_angle = (minute + 1) * 6 - 90
else:
minute_angle = minute * 6 - 90
if remaining_seconds <= 0:
break
update_clock(canvas, minute_angle, second_angle, minute_hand, second_hand)
canvas.delete("time_display")
canvas.delete("time_rectangle")
if second % 2 == 0:
draw_sawtooth_circle(canvas, 400, 400, 80, 20, "green", second_angle)
else:
draw_sawtooth_circle(canvas, 400, 400, 80, 20, "green", second_angle)
canvas.create_text(400, 620, text=f"{minute}:{second:02}", font=("Helvetica", 40), fill="red", tag="time_display")
text_object = canvas.find_withtag("time_display")
x1, y1, x2, y2 = canvas.bbox(text_object)
canvas.create_rectangle(x1 - 5, y1 - 5, x2 + 5, y2 + 5, outline="black", tag="time_rectangle")
root.update()
time.sleep(0.1)
else:
paused_remaining_seconds = remaining_seconds
canvas.create_text(400, 250, text="时间到!", font=("Helvetica", 40), fill="red", tag="time_END")
def start_countdown_10():
countdown(10 * 60, root)
def start_countdown_30():
countdown(30 * 60, root)
def start_countdown_45():
countdown(45 * 60, root)
def start_countdown_50():
countdown(50 * 60, root)
def start_countdown_10s():
countdown(10, root)
def start_countdown_1min():
countdown(60, root)
def close_program():
root.destroy()
def pause_resume():
global is_paused
global paused_remaining_seconds
is_paused = not is_paused
if not is_paused:
countdown(paused_remaining_seconds, root)
root = tk.Tk()
root.title("倒计时选择")
top_frame = tk.Frame(root)
top_frame.pack(side=tk.TOP)
bottom_frame = tk.Frame(root)
bottom_frame.pack(side=tk.BOTTOM)
canvas = tk.Canvas(bottom_frame, width=800, height=800, bg="white")
canvas.pack()
for i in range(60):
angle = i * 6 - 90
x = 320 * math.cos(math.radians(angle)) + 400
y = 320 * math.sin(math.radians(angle)) + 400
if i % 5 == 0:
canvas.create_text(x, y - 10, text=str(i), font=("Helvetica", 20))
canvas.create_line(x - 7, y, x + 7, y, width=5)
else:
canvas.create_oval(x - 2, y - 2, x + 2, y + 2, fill="blue")
canvas.create_line(x - 4, y, x + 4, y, fill="blue", width=2)
minute_hand = canvas.create_line(400, 400, 400, 100, width=6, fill="green", arrow='last')
second_hand = canvas.create_line(400, 400, 400, 80, width=2, fill="red", arrow='last')
button_10 = tk.Button(root, text="倒计时 10 分钟", command=start_countdown_10)
button_10.pack(side=tk.LEFT)
button_30 = tk.Button(root, text="倒计时 30 分钟", command=start_countdown_30)
button_30.pack(side=tk.LEFT)
button_45 = tk.Button(root, text="倒计时 45 分钟", command=start_countdown_45)
button_45.pack(side=tk.LEFT)
button_50 = tk.Button(root, text="倒计时 50 分钟", command=start_countdown_50)
button_50.pack(side=tk.LEFT)
button_10s = tk.Button(root, text="倒计时 10 秒", command=start_countdown_10s)
button_10s.pack(side=tk.LEFT)
button_1min = tk.Button(root, text="倒计时 1 分钟", command=start_countdown_1min)
button_1min.pack(side=tk.LEFT)
button_close = tk.Button(root, text="关闭程序", command=close_program)
button_close.pack(anchor=tk.SE)
root.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/28491.html