Search code examples
pythonpython-3.xtkintertimerclock

stop the clock within the time limit in tkinter python


I want to stop clock within the time limit, here i have given 5 seconds but the clock is not stopping, kindly help.

import tkinter as tk from tkinter.constants import *

def start(): global hours, minutes, seconds

if hours == 4:
    return
seconds -= 1


if seconds == 00:
    minutes -= 1
    seconds = 60

if minutes == 00 and seconds == 00:
    hours = hours+1

clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')

root.after(1000, start)

root=tk.Tk() clock = tk.Label(root, font=("bold",20), anchor=CENTER, text="00:00:00")

clock.place(relx=0.5, rely=0.5, anchor=CENTER)

hours,minutes,seconds = 0,0,5

start()

root.mainloop()


Solution

  • Just add the variable stop and if stop == True then the clock will stop

    Solution to your Question

    import tkinter as tk
    from tkinter.constants import *
    
    def start():
        global hours, minutes, seconds, stop
    
        if hours == 4:
            return
        if stop == False:
            seconds -= 1
            
        if seconds == 00:
            stop = True
        
        elif seconds == 00 and minutes != 00 and hours != 00:
            minutes -= 1
            seconds = 60
    
        elif minutes == 00 and seconds == 00:
            hours = hours+1
    
        clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')
    
        root.after(1000, start)
    
    root=tk.Tk()
    clock = tk.Label(root, font=("bold",20), anchor=CENTER, text="00:00:00")
    
    clock.place(relx=0.5, rely=0.5, anchor=CENTER)
    
    hours,minutes,seconds,stop = 0,0,5, False
    
    start()
    
    root.mainloop()
    

    a Complete clock down counter BONUS

    import tkinter as tk
    from tkinter.constants import *
    
    def start():
        global hours, minutes, seconds, stop
    
        if hours == 4:
            return
        if stop == False:
            seconds -= 1
    
        if seconds == 00 and minutes == 00 and hours == 00:
            stop = True
        
        elif seconds == -1 and minutes != 00:
            minutes -= 1
            seconds = 59
    
        elif hours != 00 and minutes == 00 and seconds == -1:
            hours -= 1
            minutes = 59
            seconds = 59
    
    
        clock.config(text=f'{hours:02}:{minutes:02}:{seconds:02}')
    
        root.after(1000, start)
    
    root=tk.Tk()
    clock = tk.Label(root, font=("bold",20), anchor=CENTER, text="00:00:00")
    
    clock.place(relx=0.5, rely=0.5, anchor=CENTER)
    
    hours,minutes,seconds,stop = 0,0,5, False
    
    start()
    
    root.mainloop()