You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Tkinter开发如何去除窗口背景实现透明显示效果

Tkinter透明窗口时钟实现方案

要实现仅展示时钟数字、完全隐藏窗口背景与桌面融合的效果,只需要给窗口配置指定透明色属性,将所有不需要显示的背景区域颜色设置为该透明色即可,修改后的可运行代码如下:

from tkinter import *
import time

master = Tk()
master.title("Clock")
# 隐藏窗口标题栏与边框
master.overrideredirect(1)
# 指定品红色为全局透明色,所有该颜色区域会完全穿透显示桌面
master.wm_attributes('-transparentcolor', '#ff00ff')
# 可选配置:让时钟始终悬浮在所有窗口顶层
master.wm_attributes('-topmost', True)

def get_time():
    timeVar = time.strftime("%I:%M:%S %p")
    clock.config(text=timeVar)
    clock.after(200,get_time)

# 标签背景设置为和全局透明色一致的品红色
clock = Label(master, font=("Calibri", 70), bg="#ff00ff", fg="blue")
clock.pack()

get_time()
master.mainloop()

核心修改说明:

  • 新增master.wm_attributes('-transparentcolor', '#ff00ff')配置项,指定不常用的品红色作为透明标识色
  • 将Label控件的背景色修改为和全局透明色一致,实现标签背景隐藏
  • 可选添加置顶配置,避免时钟被其他打开的窗口遮挡

注意不要将文本颜色设置为你指定的透明色,否则文本内容也会被透明隐藏。该方案在Windows桌面环境下可直接运行生效。

内容的提问来源于stack exchange,提问作者BlueCircle_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 07:45:07