以下是一个使用Tkinter实现的简单的电子时钟程序,可以显示当前的时间和日期,并且可以设置为透明且没有标题栏。
import tkinter as tk
import time
# 创建窗口
window = tk.Tk()
# 设置窗口属性
window.overrideredirect(True) # 隐藏标题栏
window.wm_attributes("-transparentcolor", "white") # 设置透明背景
# 创建标签用于显示时间和日期
label = tk.Label(window, font=("Arial", 80), fg='black', bg='white')
label.pack()
def update_clock():
# 获取当前时间
current_time = time.strftime('%H:%M:%S')
current_date = time.strftime('%Y-%m-%d')
# 更新标签显示
label.config(text=f"{current_date}\n{current_time}")
# 每1000毫秒更新一次时间
window.after(1000, update_clock)
# 启动更新时间的函数
update_clock()
# 运行窗口
window.mainloop()
请注意,这个程序运行时会新建一个窗口,这个窗口是可以移动和关闭的。如果想要完全隐藏窗口边框和关闭按钮,可以使用第三方库pygetwindow
。
首先,需要安装pygetwindow
库:
pip install pygetwindow
然后,修改程序如下:
import tkinter as tk
import time
import pygetwindow as gw
# 创建窗口
window = tk.Tk()
# 设置窗口属性
window.overrideredirect(True) # 隐藏标题栏
window.wm_attributes("-transparentcolor", "white") # 设置透明背景
# 创建标签用于显示时间和日期
label = tk.Label(window, font=("Arial", 80), fg='black', bg='white')
label.pack()
def update_clock():
# 获取当前时间
current_time = time.strftime('%H:%M:%S')
current_date = time.strftime('%Y-%m-%d')
# 更新标签显示
label.config(text=f"{current_date}\n{current_time}")
# 每1000毫秒更新一次时间
window.after(1000, update_clock)
# 启动更新时间的函数
update_clock()
# 运行窗口
window.mainloop()
# 隐藏窗口边框和关闭按钮
window = gw.getWindowsWithTitle('tk')[0]
window.minimize()
window.maximize()
这样修改后,程序运行时会隐藏窗口边框和关闭按钮,只显示一个透明的时间日期标签。