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

如何让Python编写的Tkinter时钟应用在Windows 11上始终置顶?

实现Tkinter时钟窗口在Windows 11上始终置顶

要让你的时钟窗口始终显示在其他窗口最上层,只需在创建Tkinter窗口对象后,添加一行窗口属性设置代码即可:

root.attributes('-topmost', True)

这行代码会告诉Tkinter将窗口设置为“总是置顶”状态,在Windows 11系统下可以正常生效。

修改后的完整源码如下:

# Source: https://www.geeksforgeeks.org/python-create-a-digital-clock-using-tkinter/

# importing whole module
from tkinter import *
from tkinter.ttk import *

# importing strftime function to
# retrieve system's time
from time import strftime

# creating tkinter window
root = Tk()
root.title('Clock')
# 设置窗口始终置顶
root.attributes('-topmost', True)

# This function is used to
# display time on the label

def time():
    string = strftime('%H:%M:%S %p')
    lbl.config(text=string)
    lbl.after(1000, time)

# Styling the label widget so that clock
# will look more attractive
lbl = Label(root, font=('calibri', 40, 'bold'),
            background='purple',
            foreground='white')

# Placing clock at the centre
# of the tkinter window
lbl.pack(anchor='center')
time()

mainloop()

如果你之后需要取消置顶状态,可以在代码中调用root.attributes('-topmost', False)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 07:09:55