-1

I'm doing a timer on Tkinter but so far I haven't found a good way to implement a countdown. I've tried with datetime and time but couldn't specifically do a 72 hour countdown. Instead of time going down, it only increases according to the local time of the machine. Can you help me?

Code and Image:

import tkinter as tk
import time

countdown = 259200

class Application():
    def __init__(self):
        self.main = tk.Tk()
        self.main.geometry('700x500')
        self.main.title('')
        self.main.resizable(0,0)
        self.CreateWidgets()
        self.main.mainloop()

    def CreateWidgets(self):
        self.now = tk.StringVar()
        self.framemain = tk.Frame(self.main)
        self.framemainlabel = tk.Frame(self.framemain,bg='black')
        self.frametime = tk.Frame(self.framemain,bg='grey')
        self.frametiming = tk.Frame(self.frametime,bg='red')

        self.timeto = tk.Label(self.frametime,text="TIME LEFT",bg='grey',font=('Trebuchet MS',30),anchor=tk.NW)

        self.time = tk.Label(self.frametime,text=self.now,bg='red',font=('Alarm Clock',100))

        self.framemain.place(relwidth=1,relheight=1)
        self.frametime.place(relwidth=1,relheight=0.8,rely=0.2)
        self.framemainlabel.place(relwidth=1,relheight=0.2)
        self.frametiming.place()

        self.timeto.pack()
        self.time.pack()

        self.Counting()

    def Counting(self):
        t=time.strftime('%I:%M:%S',time.localtime())
        if t!='':
            self.time.config(text=t)
        self.main.after(100,self.Counting)

app = Application()

Timer

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I think you could write a function that decrements by 1 the remaining time (in seconds) every time it's called, and it calls itself for the next second as shown here: https://mail.python.org/pipermail/tutor/2011-November/087134.html – Guimoute Oct 15 '19 at 19:13
  • In this case I should convert the seconds of the countdown variable to an H: M: S format. Do you have any idea how this can be done? – RetroNietzsche Oct 16 '19 at 01:53
  • There's many questions about timers and clocks and stopwatches with tkinter on this site. Have you done any research? – Bryan Oakley Oct 17 '19 at 03:16

1 Answers1

1

I had implemented a similar countdown timer for 2 minutes and 2 seconds. Attached the code below:

class UI(object):
    def __init__(self, master, **kwargs):
        self.timeLabel = tk.Label(self.master, text="02:00", font=("Times New Roman", 33), wraplength=self.screenwidth)
        self.timeRemaining = "02:02"
        self.timeLabel.pack()
        self.update_timer()

    def update_timer(self):
        self.after_id = self.master.after(1000, self.update_timer)
        self.timeRemaining = str(datetime.datetime.strftime(datetime.datetime.strptime(self.timeRemaining, '%M:%S')-datetime.timedelta(seconds=1), '%M:%S'))
        self.timeLabel.config(text=self.timeRemaining)
        if self.timeRemaining == "00:00":
            # Exit logic here   

For a 72 hour countdown I reckon the arguments to strftime would change.

learner
  • 3,168
  • 3
  • 18
  • 35
  • I was able to make it work by changing the Counting function to: def Counting(self): if self.countdown>=0: t=datetime.timedelta(seconds=self.countdown) print(t) if t!='': self.time.config(text=t) self.countdown=self.countdown-1 self.main.after(1000,self.Counting) The problem is that it doesn't set the days as time, so it looks like "3 days, 00:00:00". I don't mind much, but if I implement something to save time, in case the user turns off the PC and comes back at another time, it's hard to convert to **string** and then to **datetime** again. – RetroNietzsche Oct 17 '19 at 16:12
  • @RetroNietzsche AFAIK, there is no shortcut to this but to convert it explicitly. You can check [here](https://stackoverflow.com/questions/34134971/python-format-timedelta-greater-than-24-hours-for-display-only-containing-hours) for some pointers. If you check the first answer, you get a datetime object that you can convert to string and use to reinitialise. Also string to datetime is not that hard. If you have the string in the correct format, you can use `strptime` – learner Oct 18 '19 at 05:47