How do I correctly format a target time for a countdown?
Right now I'm trying to e开发者_运维百科nter a time in 24 hour format and initiate a countdown from the current time to the specified time.
So currently the user is entering a target hour and a target minute. I'm not sure what to do with these values to begin a countdown from now until the specified time.
import datetime
now = datetime.datetime.now() # gets the time now, puts it in now
expiringHour = int(input("What hour does the meter expire? Enter 0-23 "))
if expiringHour < 0:
print("Error. Enter a correct value")
if expiringHour > 23:
print("Error. Enter a correct value")
expiringMinute = int(input("What minute does the meter expire? Enter 0-59 "))
if expiringMinute < 0:
print("Error. Enter a correct value")
if expiringMinute > 59:
print("Error. Enter a correct value")
print(expiringHour, expiringMinute)
Well, you convert the values to a datetime, and then eneter a loop where display the remaining time from now to the expiring minute, and then sleep for a while, and then display it again, until it reaches zero.
Actually, the correct answer to this involves creating a datetime.datetime object and incrementing the datetime.datetime object till we reach the finish time.
But since you said you wanted a GUI, I had used python wrapper over xdaliclock on Ubuntu to have a countdown timer. Yeah, this is not applicable if you are on a system where xdaliclock is not available.
精彩评论