Trying to properly display currently running stopwatch time so it shows Hours:Minutes:Seconds after I press the start button. My code works only until it reaches a minute then it displays just a 1, I'm not sure how I would reset seconds to 0 again and still show the minute section. I know I need to increment it but it will end up showing the wrong time. Also I'm trying to make another if statement so when 60 minutes go by it will show the hours. Overall output ex: Current run time is 1 hour, 5 minutes and 20 seconds display would show -> 01:05:20. Edit How can I stop the timer then save the current time into a string variable?
import tkinter as tink
import datetime
seconds = -1
run = False
def var_name(mark):
def value():
if run:
global seconds
# Just beore starting
if seconds == -1:
show = "Starting"
else:
show = str(seconds)
mark['text'] = show
#Increment the count after
#every 1 second
mark.after(1000, value)
seconds += 1
if (seconds > 60):
minutes = seconds // 60
seconds = 0
SecAMin = str(minutes), ":", str(seconds)
show = SecAMin
#show = str(minutes)
mark['text'] = show
value()
def Stop():
global run
start['state'] = 'normal'
stop['state'] = 'disabled'
reset['state'] = 'normal'
run = False
You can calculate the hours and minutes based on the value of seconds
and don't need to reset seconds
to 0. Also use argument instead of global variable.
def var_name(mark, seconds=-1):
def value(seconds=seconds):
if run:
# Just before starting
if seconds == -1:
show = "Starting"
else:
mins, secs = divmod(seconds, 60)
hrs, mins = divmod(mins, 60)
show = f"{hrs:02}:{mins:02}:{secs:02}"
mark['text'] = show
#Increment the count after
#every 1 second
mark.after(1000, value, seconds+1)
value()