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