tkinter电子时钟实现时间日期鼠标点击时透明无标题栏可拖动

2024-01-08 04:59:09

当谈到使用 Python 编写 GUI(图形用户界面)应用时,tkinter 是一个常用的工具包。下面是一个简单的示例,演示如何使用 tkinter 创建一个电子时钟,具有实时显示时间和日期的功能,支持鼠标点击时窗口透明,无标题栏并且可拖动。

import tkinter as tk
from time import strftime
from ctypes import windll

# 创建主窗口
root = tk.Tk()
root.title("电子时钟")
root.geometry("300x150")
root.resizable(False, False)

# 隐藏标题栏
root.overrideredirect(True)

# 使窗口可拖动
windll.user32.ReleaseCapture()
root.bind("<B1-Motion>", lambda e: root.geometry(f"+{e.x_root}+{e.y_root}"))

# 创建标签来显示时间
time_label = tk.Label(root, font=('calibri', 30, 'bold'), background='black', foreground='white')
time_label.pack(anchor='center')

# 创建标签来显示日期
date_label = tk.Label(root, font=('calibri', 12, 'bold'), background='black', foreground='white')
date_label.pack(anchor='se')

# 更新时间和日期
def time():
    string_time = strftime('%H:%M:%S %p')
    string_date = strftime('%Y-%m-%d')
    time_label.config(text=string_time)
    date_label.config(text=string_date)
    time_label.after(1000, time)  # 每隔一秒更新一次时间

time()

# 鼠标点击时窗口透明
def toggle_transparency(event):
    current_alpha = root.attributes('-alpha')
    new_alpha = 0.7 if current_alpha == 1.0 else 1.0
    root.attributes('-alpha', new_alpha)

root.bind("<Button-1>", toggle_transparency)

# 运行主循环
root.mainloop()

文章来源:https://blog.csdn.net/zsd2829568515/article/details/135446770
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。