Tkinter window to show count down value in a Label by starting from a fixed value
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("350x170") # width and height of the window
counter=11 # Initital value of counter
def my_time():
global counter
counter=counter-1 # decrease value by 1
if counter < 0:
return
l1.config(text=str(counter)) # Update the label text using string
l1.after(1000,my_time) # time delay of 1000 milliseconds
my_font=('times',76,'bold') # display size and style
l1=tk.Label(my_w,font=my_font,bg='yellow',width=2)
l1.grid(row=1,column=1,padx=50,pady=30)
my_time() # call the function
my_w.mainloop()
Adding Reset button
To start the counter again from any value , we can add one reset button. Place this code above the line my_w.mainloop()
def restart():
global counter
counter=11 # start value
my_time() # call the function
b1=tk.Button(my_w,text='Restart',font=22,command=lambda:restart())
b1.grid(row=1,column=2,padx=10)