简单的Tkinter电子时钟实现,包括时间和日期,并且窗口可以设置为透明无标题栏

本文介绍了一个使用Python的Tkinter库创建的简单时钟应用,包含时间与日期显示,窗口透明且无标题栏,通过定时更新功能实时展示当前时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import tkinter as tk
import time

class Clock:
    def __init__(self, root):
        self.root = root
        self.root.overrideredirect(1)  # 设置为无标题栏
        self.root.attributes('-alpha', 0.5)  # 设置为半透明
        self.root.bind('<Button-1>', self.quit)  # 点击窗口退出

        self.label_time = tk.Label(root, font=('calibri', 40, 'bold'), bg='black', fg='white')
        self.label_time.pack(padx=20, pady=20)

        self.label_date = tk.Label(root, font=('calibri', 20), bg='black', fg='white')
        self.label_date.pack(padx=20, pady=10)

        self.update_time()

    def update_time(self):
        current_time = time.strftime('%H:%M:%S')
        self.label_time.config(text=current_time)
        self.root.after(1000, self.update_time)

    def quit(self, event):
        self.root.destroy()

root = tk.Tk()
clock = Clock(root)
root.mainloop()
 

该程序设置了一个Clock类,包含一个时间和日期标签以及一个循环更新时间的方法。在mainloop中实例化该类,然后运行Tkinter应用程序。

在初始化时,设置了一些窗口属性,如无标题栏和透明度。overrideredirect方法去除了窗口的标准标题栏,而attributes方法设置透明度,在这里设置为0.5。

时间和日期标签使用Label方法创建,并设置了一些标签属性,如字体,背景和前景颜色。update_time方法使用strftime方法获取当前时间,并将其显示在时间标签上。

最后,quit方法退出应用程序,单击窗口时调用该方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值