1

i'm trying to make a timer that can shows how many hours and minutes between now and a specify time and display it with tkinter,i want the timer update every seconds,i'm new to python i dont really know how to do it

import datetime
from datetime import datetime, timedelta
import tkinter as tk
from tkinter import Label
now = datetime.now
time = "09:00:00"
FMT = "%H:%M:%S"
timedelta = datetime.strptime(now, FMT) - datetime.strptime(time, FMT)
print(timedelta)
main = tk.Tk()
main.title("timer")
lb=Label(text=timedelta)
lb.pack()
main.mainloop()

i make alot of mistake on this script as well

16e
  • 11
  • 2
  • You did not call `datetime.now`. Add `()`! – Klaus D. Mar 22 '20 at 03:26
  • Traceback (most recent call last): File "c:/Users/jerry/Desktop/python saves/timer/test.py", line 8, in timedelta = datetime.strptime(now, FMT) - datetime.strptime(time, FMT) TypeError: must be str, not datetime.datetime – 16e Mar 22 '20 at 03:29
  • i have an error – 16e Mar 22 '20 at 03:29
  • some notes: l.5 - `datetime.now()`, don't forget the brackets. / l.6 - you specify a time but no date / l.7 - you overwrite an import, namely `timedelta`, and your variable `now` is a `datetime` object, not a string so `datetime.strptime` cannot be called on that. – FObersteiner Mar 22 '20 at 10:38

2 Answers2

2

I think this might help - How to calculate number of days between two given dates? or this - How do I find the time difference between two datetime objects in python? or maybe this - Measuring elapsed time with the Time module
I tried, and made this:

import datetime
from datetime import date

today = datetime.date.today()
yourday = datetime.date(2020, 7, 15)
timebeetwen = today - yourday
print(timebeetwen.days*24) #hours
print(timebeetwen.days*24*60) #minutes
print(timebeetwen.days*24*60*60) #seconds

Datetime works only with days so the output will be like this:

>>> 48
>>> 2880
>>> 172800

And if you want to show every second use

while True:
    print(timebeetwen.days*24) #hours
    print(timebeetwen.days*24*60) #minutes
    print(timebeetwen.days*24*60*60) #seconds
0

So I find another answer. Here is the link - How to make a timer program in Python
And here is the code:

import time
# Ask to Begin
#start = input("Would you like to begin Timing? (y/n): ")
#if start == "y":
timeLoop = True

# Variables to keep track and display
Sec = 0
Min = 0
# Begin Process
#timeLoop = start
while timeLoop:
    Sec += 1
    print(str(Min) + " Mins " + str(Sec) + " Sec ")
    time.sleep(1)
    if Sec == 60:
        Sec = 0
        Min += 1
        print(str(Min) + " Minute")

This is like a timer. It counts every second as you need