python-翻页动态时钟

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


效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值