使用tkinter 电子时钟 实现时间日期 可实现透明 无标题栏

以下是一个简单的 tkinter 电子时钟实现时间日期,可实现透明无标题栏:

import tkinter as tk
import time

class Clock(tk.Tk):
    def __init__(self):
        super().__init__()
        self.overrideredirect(True)  # 隐藏标题栏
        self.attributes("-alpha", 0.7)  # 设置窗口透明度
        self.geometry("200x50+800+500")  # 设置窗口大小和位置

        self.time_label = tk.Label(self, font=("Courier", 20, "bold"))
        self.time_label.pack()

        self.date_label = tk.Label(self, font=("Courier", 12))
        self.date_label.pack()

        self.update_clock()

    def update_clock(self):
        current_time = time.strftime("%H:%M:%S")
        self.time_label.config(text=current_time)
        current_date = time.strftime("%Y-%m-%d")
        self.date_label.config(text=current_date)
        self.after(1000, self.update_clock)

if __name__ == '__main__':
    clock = Clock()
    clock.mainloop()

我们可以看到,这个 tkinter 电子时钟使用了 tk.Label 来显示时间和日期。overrideredirect(True) 可以隐藏窗口的标题栏,attributes("-alpha", 0.7) 可以设置窗口的透明度。同样,我们也可以设置窗口的大小和位置。最后,通过 after 函数来定时更新时间和日期即可。

posted @ 2023-08-25 16:17  jixn  阅读(41)  评论(0)    收藏  举报  来源