Skip to content

Commit 06a24bf

Browse files
Countdown Timer: display time as mm:ss and initialize lavel to 00.00
1 parent fd67603 commit 06a24bf

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

CountDown Timer/main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from tkinter import *
22
import tkinter as tk
33

4+
def fmt_time(seconds: int) -> str:
5+
seconds = max(0, int(seconds))
6+
m, s = divmod(seconds, 60)
7+
return f"{m:02d}:{s:02d}"
8+
9+
410
class Application(Frame):
511
def __init__(self,master):
612
super(Application,self).__init__(master)
@@ -39,7 +45,9 @@ def createWidgets(self):
3945
self.someFrame.pack(side=TOP)
4046

4147
self.labelvariable = StringVar()
42-
self.labelvariable.set("")
48+
self.labelvariable.set(fmt_time(0))
49+
50+
4351

4452
self.thelabel = Label(self,textvariable = self.labelvariable,font=('Helvetica',50))
4553
self.thelabel.pack(side=TOP)
@@ -80,14 +88,14 @@ def closeApp(self):
8088
def countdown(self, timeInSeconds, start=True):
8189
if timeInSeconds == 0:
8290
self._starttime=0
83-
self.labelvariable.set("0")
91+
self.labelvariable.set(fmt_time(0))
8492
return
8593
if start:
8694
self._starttime = timeInSeconds
8795
if self._paused:
8896
self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds, False)
8997
else:
90-
app.labelvariable.set(timeInSeconds)
98+
app.labelvariable.set(fmt_time(timeInSeconds))
9199
self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds-1, False)
92100

93101

0 commit comments

Comments
 (0)