import tkinter as tk
from datetime import datetime
root = tk.Tk()
root.title("水平排列的带圆角正方形和电子时钟")
root.attributes('-fullscreen', True)
canvas = tk.Canvas(root, width=root.winfo_screenwidth(), height=root.winfo_screenheight())
canvas.pack()
def draw_rounded_square_with_text(x, y, w, h, radius, color, text):
x0, y0 = x, y
x1, y1 = x + w, y + h
canvas.create_arc(x0, y0, x0 + radius * 2, y0 + radius * 2, start=90, extent=90, fill=color)
canvas.create_arc(x1 - radius * 2, y0, x1, y0 + radius * 2, start=0, extent=90, fill=color)
canvas.create_arc(x0, y1 - radius * 2, x0 + radius * 2, y1, start=180, extent=90, fill=color)
canvas.create_arc(x1 - radius * 2, y1 - radius * 2, x1, y1, start=270, extent=90, fill=color)
canvas.create_rectangle(x0 + radius, y0, x1 - radius, y1, fill=color)
canvas.create_rectangle(x0, y0 + radius, x1, y1 - radius, fill=color)
canvas.create_text((x0 + x1) // 2, (y0 + y1) // 2, text=text, fill="white", font=("微软雅黑", 250))
def exit_program(event):
root.destroy()
def update_clock():
now = datetime.now()
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
canvas.delete("clock") # 清除之前的时钟显示
# 计算正方形的位置使其居中显示
square_width = 400
screenwidth=root.winfo_screenwidth()
screenheight=root.winfo_screenheight()
x_offset = (screenwidth-3*square_width)/4
y_offset = (screenheight-square_width)/2
# 第一个带圆角正方形,显示小时
draw_rounded_square_with_text(x_offset, y_offset, square_width, square_width, 50, "black", hour)
# 第二个带圆角正方形,显示分钟
draw_rounded_square_with_text(x_offset*2 + square_width, y_offset, square_width, square_width, 50, "black", minute)
# 第三个带圆角正方形,显示秒钟
draw_rounded_square_with_text(x_offset*3 + square_width * 2, y_offset, square_width, square_width, 50, "black", second)
root.after(1000, update_clock) # 每隔1秒更新时钟
update_clock()
root.bind("<Escape>", exit_program)
root.mainloop()
python-翻页动态时钟
最新推荐文章于 2024-09-01 08:15:22 发布