当前位置:网站首页 > 编程语言 > 正文

桌面时钟代码(电脑代码时钟)



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()

到此这篇桌面时钟代码(电脑代码时钟)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在 编程的领域有一番成就!

版权声明


相关文章:

  • 单播地址,组播地址,广播地址的区别(简述单播地址组播地址和广播地址的定义和作用)2025-04-16 08:27:05
  • ip地址换了怎么连打印机(ip地址换了打印机怎么联)2025-04-16 08:27:05
  • junit5怎么下载(junit4怎么导入)2025-04-16 08:27:05
  • 工具类英文(工具类英文公司名)2025-04-16 08:27:05
  • pfna是什么材料做的(pfa是什么材料?)2025-04-16 08:27:05
  • vbfi游戏(vbf什么游戏)2025-04-16 08:27:05
  • enemies翻译(ensemble翻译)2025-04-16 08:27:05
  • tip期刊投稿(tie 期刊)2025-04-16 08:27:05
  • 怎么添加bigboss源(bigboss源可以删掉吗)2025-04-16 08:27:05
  • 返回下一级目录(返回上级目录)2025-04-16 08:27:05
  • 全屏图片