2

I have this clock app written in python. It runs successfully on Windows.

Here is the source code.

# 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')


# 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()

This is how the clock app looks.

enter image description here

It works fine. However, I want to make the app always appear on top in Windows. How can I modify the code to make the app always appear on top?

I am using Windows 11.

user3848207
  • 3,737
  • 17
  • 59
  • 104
  • Does this answer your question? [How to bring Tkinter window in front of other windows?](https://stackoverflow.com/questions/28312550/how-to-bring-tkinter-window-in-front-of-other-windows) – Swifty Aug 18 '23 at 10:55
  • Does this answer your question? [How to make a Tkinter window jump to the front?](https://stackoverflow.com/questions/1892339/how-to-make-a-tkinter-window-jump-to-the-front) – Henry Woody Aug 18 '23 at 11:12

3 Answers3

4

I found the answer to my own question. Add the line below.

# make the window always on top
root.attributes('-topmost', True)
user3848207
  • 3,737
  • 17
  • 59
  • 104
2

By adding the line root.attributes('-topmost', True), you set the window to be always on top of other windows.

Update your code as follow:

# 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)  # Set the window to always appear on top



# 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()
Baris Sonmez
  • 477
  • 2
  • 8
2

Before starting mainloop() add this line in your code to make the app always appear on top in Windows :

root.wm_attributes("-topmost", 1)

This will help you to achieve desired result.