stop the clock within the time limit in tkinter python

Question:

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

Asked By: Sandeep

||

Answers:

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()
Answered By: Abdo Sabry

Solution without that many if conditions. I use a timestamp, but I have also some trouble with the timezone I think. Maybe someone can solve my offsets.

My tkinter TIMER app, with bell:

import tkinter as tk
import calendar
from datetime import datetime
import time
import sys 


class Countdown(tk.Frame):
    def __init__(self,master):
        tk.Frame.__init__(self, master)
        global target
        self.master = master
        self.clock = tk.Label(text="00:00:00", fg="Green", font=("bold",40))
        self.clock.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
        timestamp = calendar.timegm(time.localtime())
        print("Timestamp",timestamp)
        print("Timestamp_future",timestamp + t_plus)
        target = timestamp + t_plus
        self.update_clock()
       
    def update_clock(self):     
        current = calendar.timegm(time.localtime()) -23*3600
        countdown = target - current 
        self.down_time = datetime.fromtimestamp(countdown).strftime("%H:%M:%S")       
        self.clock.configure(text=self.down_time)
        if countdown/3600 != 23:
            self.after(1000, self.update_clock)
        else:
            print('a', end='', flush=True) 

            
root = tk.Tk()
global t_plus

######### SET YOUR TIME FOR THE COUNTDOWN ############
your_timer_set = '00:00:05' # hours, minutes, seconds
######################################################
countdown = time.strptime(your_timer_set, '%H:%M:%S')
hours = int(countdown.tm_hour)
minutes = int(countdown.tm_min)
seconds = int(countdown.tm_sec)
t_plus = ((hours*3600)+(minutes*60)+seconds)
print("SET_TIME",t_plus)

app=Countdown(root)
root.title("Countdown")
root.geometry("400x400+400+150")

root.mainloop()
Answered By: Hermann12