|
| 1 | +from tkinter import * |
| 2 | +import tkinter as tk |
| 3 | + |
| 4 | +class Application(Frame): |
| 5 | + def __init__(self,master): |
| 6 | + super(Application,self).__init__(master) |
| 7 | + self.pack() |
| 8 | + self.createWidgets() |
| 9 | + |
| 10 | + self.master.geometry("300x200") |
| 11 | + self._alarm_id = None |
| 12 | + self._paused = False |
| 13 | + self._starttime = 0 |
| 14 | + |
| 15 | + # def startTimer(self): |
| 16 | + |
| 17 | + def createWidgets(self): |
| 18 | + |
| 19 | + self.someFrame = Frame(self) |
| 20 | + self.time_var = StringVar() |
| 21 | + self.input_time = tk.Entry(self, width=25, font=('Calibri',12), textvariable = self.time_var) |
| 22 | + # Adding placeholder |
| 23 | + self.input_time.insert(0, "Enter Time in seconds") |
| 24 | + self.input_time.pack(padx=10,pady=(30,10)) |
| 25 | + #using bind method |
| 26 | + self.input_time.bind("<Button-1>", self.enter) |
| 27 | + |
| 28 | + self.startButton = Button(self.someFrame, text="Start", font=('Helvetica',12), bg='green', fg='white', command=self.startTime) |
| 29 | + self.startButton.pack(side=LEFT, padx=5) |
| 30 | + |
| 31 | + self.pauseButton = Button(self.someFrame, text="Pause", font=('Helvetica',12), bg='azure', command=self.pauseTime) |
| 32 | + self.pauseButton.pack(side=LEFT, padx=5) |
| 33 | + |
| 34 | + self.resetButton = Button(self.someFrame, text="Reset", font=('Helvetica',12), bg='azure', command=self.resetTime) |
| 35 | + self.resetButton.pack(side=LEFT, padx=5) |
| 36 | + |
| 37 | + self.closeButton = Button(self.someFrame, text="Close", font=('Helvetica',12), bg='red',fg='white', command=self.closeApp) |
| 38 | + self.closeButton.pack(side=LEFT, padx=5) |
| 39 | + self.someFrame.pack(side=TOP) |
| 40 | + |
| 41 | + self.labelvariable = StringVar() |
| 42 | + self.labelvariable.set("") |
| 43 | + |
| 44 | + self.thelabel = Label(self,textvariable = self.labelvariable,font=('Helvetica',50)) |
| 45 | + self.thelabel.pack(side=TOP) |
| 46 | + |
| 47 | + # Removes placeholder text in the Entry widget |
| 48 | + def enter(self,*args): |
| 49 | + self.input_time.delete(0, 'end') |
| 50 | + |
| 51 | + # Resume function |
| 52 | + def startTime(self): |
| 53 | + self.get_time = self.time_var.get() |
| 54 | + try: |
| 55 | + self.time_in_int = int(self.get_time) |
| 56 | + except: |
| 57 | + self.time_in_int = 0 |
| 58 | + self._starttime = self.time_in_int |
| 59 | + self._paused = False |
| 60 | + if self._alarm_id is None: |
| 61 | + self.countdown(self._starttime) |
| 62 | + |
| 63 | + # Pause function |
| 64 | + def pauseTime(self): |
| 65 | + if self._alarm_id is not None: |
| 66 | + self._paused = True |
| 67 | + |
| 68 | + # Reset function |
| 69 | + def resetTime(self): |
| 70 | + if self._alarm_id is not None: |
| 71 | + self.master.after_cancel(self._alarm_id) |
| 72 | + self._alarm_id = None |
| 73 | + self._paused = False |
| 74 | + self.countdown(0) |
| 75 | + self._paused = True |
| 76 | + |
| 77 | + def closeApp(self): |
| 78 | + self.master.destroy() |
| 79 | + |
| 80 | + def countdown(self, timeInSeconds, start=True): |
| 81 | + if timeInSeconds == 0: |
| 82 | + self._starttime=0 |
| 83 | + self.labelvariable.set("0") |
| 84 | + return |
| 85 | + if start: |
| 86 | + self._starttime = timeInSeconds |
| 87 | + if self._paused: |
| 88 | + self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds, False) |
| 89 | + else: |
| 90 | + app.labelvariable.set(timeInSeconds) |
| 91 | + self._alarm_id = self.master.after(1000, self.countdown, timeInSeconds-1, False) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + root = Tk() |
| 96 | + root.title("Countdown Timer") |
| 97 | + app = Application(root) |
| 98 | + root.mainloop() |
| 99 | + |
0 commit comments