Advertisement
AceScottie

alarm_clock_class.py

Jan 8th, 2020
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. from tkinter import *
  2. import time
  3. import os
  4.  
  5. class GUI:
  6.     def __init__(self):
  7.         self.quitting = False
  8.         self.alarm_state = False
  9.         self.win = Tk()
  10.         self.win.title("Clock")
  11.         self.win.protocol("WM_DELETE_WINDOW", self.quitter) ## runs the function when the X button is pressed.
  12.  
  13.     def quitter(self): # function controls how the application should close
  14.         self.quitting = True
  15.  
  16.     def button(self): #Triggers the application to start checking the alarm to see if it has elapsed
  17.         self.alarm_state = True
  18.  
  19.     def check_alarm(self):
  20.         if self.alarm.get()==time.strftime('%H:%M:%S'):
  21.             print("Times Up!")
  22.             self.alarm_state = False
  23.  
  24.     def run(self):
  25.         timer = Label(self.win,font=('times', 28, 'bold'), background='blue', foreground='white') ##renamed this to be easier to read
  26.         timer.pack(fill=BOTH, expand=1)
  27.         self.alarm=Entry(self.win)
  28.         self.alarm.pack()
  29.         bt=Button(self.win, text='Set alarm', command=self.button).pack()
  30.         while not self.quitting:
  31.             self.win.update()
  32.             self.win.update_idletasks()
  33.             timer["text"]  = time.strftime('%H:%M:%S')
  34.             if self.alarm_state:
  35.                 self.check_alarm()
  36.  
  37. if __name__ == "__main__":
  38.     app= GUI() ##creates the app as an object
  39.     try:
  40.         app.run() ##runs the application
  41.     except KeyboardInterrupt:
  42.         app.quitter() ##safly quits the application if ctrl+c is pressed
  43.     except:
  44.         raise ##displays any other errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement