内容中心

2026年评价高的昆明税务筹划合规审查行业公司推荐-云南慧力量企业管理有限公司

To create an analog clock with Tkinter, we'll use canvas drawing to render the clock face, numbers, and moving hands (second, minute, hour). The hands update every second using the current system time.

Step-by-Step Explanation:

  1. Imports: Use tkinter for the GUI, datetime to get current time, and math for trigonometric calculations.
  2. Window & Canvas: Create a main window and a canvas to draw the clock.
  3. Clock Face: Draw a circle for the clock face and numbers 1-12 around it.
  4. Hands Calculation: Compute angles for each hand using current time (seconds → 6°/sec, minutes →6°/min +0.1°/sec, hours →30°/hour +0.5°/min +1/120°/sec).
  5. Update: Redraw the clock every second to reflect the current time.

Full Code:

import tkinter as tk
from datetime import datetime
import math

# Main window setup
root = tk.Tk()
root.title("Analog Clock")

# Canvas dimensions
canvas_width = 400
canvas_height = 400
canvas = tk.Canvas(root, width=canvas_width, height=canvas_height, bg="white")
canvas.pack()

# Clock center and radius
center_x = canvas_width // 2
center_y = canvas_height // 2
clock_radius = 180

def draw_clock():
    # Clear previous drawing
    canvas.delete("all")

    # Draw clock face (outer circle)
    canvas.create_oval(
        center_x - clock_radius, center_y - clock_radius,
        center_x + clock_radius, center_y + clock_radius,
        outline="black", width=2
    )

    # Draw numbers 1-12
    for num in range(1, 13):
        angle = (num * 30) * math.pi / 180 - math.pi/2  # Adjust to top (12 o'clock)
        x = center_x + (clock_radius -20)*math.cos(angle)
        y = center_y + (clock_radius -20)*math.sin(angle)
        canvas.create_text(x, y, text=str(num), font=("Arial",14,"bold"))

    # Get current time
    now = datetime.now()
    hours = now.hour
    minutes = now.minute
    seconds = now.second

    # Draw second hand (red, thin, longest)
    sec_angle = (seconds *6) * math.pi/180 - math.pi/2
    sec_x = center_x + (clock_radius -10)*math.cos(sec_angle)
    sec_y = center_y + (clock_radius -10)*math.sin(sec_angle)
    canvas.create_line(center_x, center_y, sec_x, sec_y, fill="red", width=1)

    # Draw minute hand (blue, medium thickness)
    min_angle = (minutes*6 + seconds*0.1) * math.pi/180 - math.pi/2
    min_x = center_x + (clock_radius -20)*math.cos(min_angle)
    min_y = center_y + (clock_radius -20)*math.sin(min_angle)
    canvas.create_line(center_x, center_y, min_x, min_y, fill="blue", width=2)

    # Draw hour hand (black, thickest, shortest)
    hour_angle = ((hours%12)*30 + minutes*0.5 + seconds*(0.5/60)) * math.pi/180 - math.pi/2
    hour_x = center_x + (clock_radius -40)*math.cos(hour_angle)
    hour_y = center_y + (clock_radius -40)*math.sin(hour_angle)
    canvas.create_line(center_x, center_y, hour_x, hour_y, fill="black", width=3)

    # Draw center dot
    canvas.create_oval(center_x-5, center_y-5, center_x+5, center_y+5, fill="black")

    # Update every 1 second
    root.after(1000, draw_clock)

# Start the clock
draw_clock()
root.mainloop()

Key Features:

  • Smooth Movement: The minute and hour hands move gradually (not just at full minutes/hours).
  • Clear Visuals: Distinct colors for each hand (red: second, blue: minute, black: hour).
  • Accurate Time: Uses system time to update hands every second.

Run the code to see the analog clock in action! It will display a white clock face with black numbers and moving hands that reflect the current time. The window is resizable, but the clock will stay centered in the canvas.

云南慧力量企业管理有限公司

云南慧力量企业管理有限公司



作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?