(最近尝试请AI帮忙编语句,用python或shell实现一些简单任务。方便日常工作和学习。)
语句:
———————
import tkinter as tk
from tkinter import ttk
import math
from datetime import datetime
import time
class AnalogClock:
def __init__(self, root):
self.root = root
self.root.title("圆盘时钟")
self.root.geometry("400x450")
self.root.resizable(False, False)
# 设置画布
self.canvas = tk.Canvas(root, width=400, height=400, bg='white')
self.canvas.pack()
# 中心点和半径
self.center_x = 200
self.center_y = 200
self.radius = 180
# 绘制静态的表盘
self.draw_clock_face()
# 启动更新
self.update_clock()
def draw_clock_face(self):
"""绘制表盘刻度、数字和边框"""
# 外圆
self.canvas.create_oval(
self.center_x - self.radius, self.center_y - self.radius,
self.center_x + self.radius, self.center_y + self.radius,
width=3, outline='black'
)
# 内圆(装饰)
self.canvas.create_oval(
self.center_x - self.radius + 10, self.center_y - self.radius + 10,
self.center_x + self.radius - 10, self.center_y + self.radius - 10,
width=1, outline='gray'
)
# 绘制刻度和数字
for i in range(60):
angle = math.radians(i * 6 - 90)
if i % 5 == 0: # 小时刻度
# 长刻度线
x1 = self.center_x + (self.radius - 20) * math.cos(angle)
y1 = self.center_y + (self.radius - 20) * math.sin(angle)
x2 = self.center_x + self.radius * math.cos(angle)
y2 = self.center_y + self.radius * math.sin(angle)
self.canvas.create_line(x1, y1, x2, y2, width=3, fill='black')
# 数字
num = i // 5 if i > 0 else 12
num_x = self.center_x + (self.radius - 40) * math.cos(angle)
num_y = self.center_y + (self.radius - 40) * math.sin(angle)
self.canvas.create_text(num_x, num_y, text=str(num),
font=('Arial', 16, 'bold'))
else: # 分钟刻度
x1 = self.center_x + (self.radius - 10) * math.cos(angle)
y1 = self.center_y + (self.radius - 10) * math.sin(angle)
x2 = self.center_x + self.radius * math.cos(angle)
y2 = self.center_y + self.radius * math.sin(angle)
self.canvas.create_line(x1, y1, x2, y2, width=1, fill='gray')
# 中心点
self.canvas.create_oval(
self.center_x - 5, self.center_y - 5,
self.center_x + 5, self.center_y + 5,
fill='black'
)
def update_clock(self):
"""更新指针位置"""
# 清除旧指针
self.canvas.delete("hands")
now = datetime.now()
hours = now.hour % 12
minutes = now.minute
seconds = now.second
ms = now.microsecond / 1000000 # 毫秒用于平滑移动
# 计算角度(顺时针,12点为0度)
# 秒针:平滑移动
second_angle = math.radians((seconds + ms) * 6 - 90)
# 分针:随秒针微动
minute_angle = math.radians((minutes + seconds/60) * 6 - 90)
# 时针:随分针微动
hour_angle = math.radians((hours + minutes/60) * 30 - 90)
# 绘制时针
hour_x = self.center_x + (self.radius * 0.5) * math.cos(hour_angle)
hour_y = self.center_y + (self.radius * 0.5) * math.sin(hour_angle)
self.canvas.create_line(
self.center_x, self.center_y, hour_x, hour_y,
width=6, fill='black', tags="hands", capstyle=tk.ROUND
)
# 绘制分针
minute_x = self.center_x + (self.radius * 0.7) * math.cos(minute_angle)
minute_y = self.center_y + (self.radius * 0.7) * math.sin(minute_angle)
self.canvas.create_line(
self.center_x, self.center_y, minute_x, minute_y,
width=4, fill='blue', tags="hands", capstyle=tk.ROUND
)
# 绘制秒针
second_x = self.center_x + (self.radius * 0.85) * math.cos(second_angle)
second_y = self.center_y + (self.radius * 0.85) * math.sin(second_angle)
self.canvas.create_line(
self.center_x, self.center_y, second_x, second_y,
width=2, fill='red', tags="hands", capstyle=tk.ROUND
)
# 中心盖
self.canvas.create_oval(
self.center_x - 4, self.center_y - 4,
self.center_x + 4, self.center_y + 4,
fill='red', tags="hands"
)
# 显示数字时间
self.canvas.delete("digital")
time_str = now.strftime("%H:%M:%S")
self.canvas.create_text(
self.center_x, self.center_y + self.radius + 20,
text=time_str, font=('Arial', 14), tags="digital"
)
# 50毫秒刷新一次,实现秒针平滑移动
self.root.after(50, self.update_clock)
if __name__ == "__main__":
root = tk.Tk()
clock = AnalogClock(root)
root.mainloop()