如何让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
相关产品推荐
相关产品推荐

