Advertisement
AceScottie

alarm.py

Jan 13th, 2020
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.70 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. from datetime import datetime, timedelta
  4. ##custom imports
  5. from aceutil import TkUtils, Log #download from https://pastebin.com/yUaMxmv3
  6. from tkutils import * #download from https://pastebin.com/TLdsJ2er
  7.  
  8. class TKGUI:
  9.     def __init__(self):
  10.         self.log = Log("Alarm Clock")
  11.  
  12.         self.root= Tk() ##initilise the main window
  13.         self.tku = TkUtils(self.log, self.root)
  14.         self.quit = False ##defaults the quit flag
  15.         self.multi_alarms = {}
  16.         self.next_alarms = {}
  17.  
  18.  
  19.     def add_alarm(self, event, date=[], time=[], days=[], ty=""):
  20.         if ty == "Multi":
  21.             al_time = time[0].get()+":"+time[1].get()+":00"
  22.             d = [0] * len(days)
  23.             for i in range(len(days)):
  24.                 d[i] = days[i].get()
  25.             if len(self.multi_alarms)  < 1:
  26.                 self.multi_alarms[0] = [al_time] + d
  27.             else:
  28.                 self.multi_alarms[max(self.multi_alarms.keys())+1] = [al_time] + d
  29.         self.list_alarms()
  30.  
  31.     def add_alarm_window(self, a=None, b=None, c=None, event=Event(), al_type=None):
  32.         over = self.tku.overlay(self.root, Event(), "Add Alarms", height=200, width=320)
  33.         window = self.tku.cFrame(over, padx=5, pady=5, fill=BOTH, expand=1, bg="white")
  34.         holder = self.tku.cFrame(window, side=TOP, fill=BOTH, expand=1, bg="white")
  35.         can = Canvas(holder, bg="white", highlightthickness=0)
  36.         can.pack(side=TOP, fill=BOTH, expand=1)
  37.         holder2 = self.tku.cFrame(can, fill=X)
  38.         scroll, can2 = self.tku.scrollable_area2(can)
  39.         holder3= self.tku.cFrame(scroll, side=TOP, borderwidth=1, relief="groove")
  40.  
  41.         al = {}
  42.         tyF = self.tku.cFrame(holder3, side=TOP, fill=X)
  43.         Label(tyF, text="Type", justify=RIGHT, anchor="e", bg="white", borderwidth=1, relief="groove", width=10).pack(side=LEFT)
  44.         al_types = ["Single", "Multi"]
  45.         alV= StringVar(tyF)
  46.         if al_type == None:
  47.             alV.set(None)
  48.         else:
  49.             alV.set(al_type.get())
  50.         al['type'] = OptionMenu(*(tyF, alV) + tuple(al_types))
  51.         al['type'].pack(side=LEFT)
  52.  
  53.         alV.trace("w", lambda a, b, c, e=Event(), t=alV: self.add_alarm_window(a, b, c, event, t))
  54.  
  55.         dt_picker = self.tku.cFrame(holder3, fill=BOTH, expand=1)
  56.         if al_type != None:
  57.             if al_type.get() == "Multi":
  58.                 lables = self.tku.cFrame(holder3, fill=X, expand=1)
  59.                 w=4
  60.                 Label(dt_picker, text="Time", borderwidth=1, relief="raised", width=10).pack(side=LEFT)
  61.                 Label(dt_picker, text="M", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  62.                 Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  63.                 Label(dt_picker, text="W", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  64.                 Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  65.                 Label(dt_picker, text="F", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  66.                 Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  67.                 Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  68.                 options = self.tku.cFrame(holder3, fill=X, expand=1)
  69.                 hhV = StringVar()
  70.                 hh = MaxLengthEntry(options, maxlength=2, valtype="int/hour", width=3, textvariable=hhV)
  71.                 hh.pack(side=LEFT)
  72.                 Label(options, text=":", width=3).pack(side=LEFT)
  73.                 mmV = StringVar()
  74.                 mm = MaxLengthEntry(options, maxlength=2, valtype="int/minute", width=3, textvariable=mmV)
  75.                 mm.pack(side=LEFT)
  76.                 ckw = 1
  77.                 Monv =  IntVar()
  78.                 Mon = Checkbutton(options, variable=Monv, width=ckw, borderwidth=1, relief="groove")
  79.                 Mon.pack(side=LEFT)
  80.  
  81.                 Tuev =  IntVar()
  82.                 Tue = Checkbutton(options, variable=Tuev, width=ckw, borderwidth=1, relief="groove")
  83.                 Tue.pack(side=LEFT)
  84.  
  85.                 Wedv =  IntVar()
  86.                 Wed = Checkbutton(options, variable=Wedv, width=ckw, borderwidth=1, relief="groove")
  87.                 Wed.pack(side=LEFT)
  88.  
  89.                 Thuv =  IntVar()
  90.                 Thu = Checkbutton(options, variable=Thuv, width=ckw, borderwidth=1, relief="groove")
  91.                 Thu.pack(side=LEFT)
  92.  
  93.                 Friv =  IntVar()
  94.                 Fri = Checkbutton(options, variable=Friv, width=ckw, borderwidth=1, relief="groove")
  95.                 Fri.pack(side=LEFT)
  96.  
  97.                 Satv =  IntVar()
  98.                 Sat = Checkbutton(options, variable=Satv, width=ckw, borderwidth=1, relief="groove")
  99.                 Sat.pack(side=LEFT)
  100.  
  101.                 Sunv =  IntVar()
  102.                 Sun = Checkbutton(options, variable=Sunv, width=ckw, borderwidth=1, relief="groove")
  103.                 Sun.pack(side=LEFT)
  104.  
  105.                 self.tku.cButton(holder3, text="Add", command=lambda e=Event(), t=[hh, mm], day=[Monv, Tuev, Wedv, Thuv, Friv, Satv, Sunv], ty="Multi": self.add_alarm(e, time=t, days=day, ty=ty))
  106.  
  107.     def open_edit(self, event, al_id):
  108.         alarm = self.multi_alarms[al_id]
  109.         print(alarm)
  110.  
  111.     def list_alarms(self):
  112.         children = self.alarms.winfo_children()
  113.         for child in children:
  114.             child.pack_forget()
  115.             child.destroy()
  116.         days = [0, "M", "T", "W", "T", "F", "S", "S"]
  117.         for key, value in self.multi_alarms.items():
  118.             row = self.tku.cFrame(self.alarms, fill=X, side=TOP)
  119.             Label(row, text=value[0]).pack(side=LEFT)
  120.             hol=Frame(row, borderwidth=1, relief="groove")
  121.             hol.pack(side=LEFT)
  122.             for x in range(1, len(value)):
  123.                 if value[x] == 1:
  124.                     Label(hol, text=days[x], fg="blue", bg="white").pack(side=LEFT)
  125.                 else:
  126.                     Label(hol, text=days[x], fg="lightgray", bg="white").pack(side=LEFT)
  127.             self.tku.cButton(row, text="Update", command=lambda e=Event(), al_id=key: self.open_edit(e, al_id))
  128.  
  129.  
  130.     def trigger_alarm(self, al_id):
  131.         print("Alarm Triggered for %s at %s" %(int(al_id), self.multi_alarms[al_id][0]))
  132.  
  133.     def trigger_alarms(self):
  134.         now = datetime.now()
  135.         complete = []
  136.         for key, value in self.multi_alarms.items():
  137.             if now.strftime("%H:%M:%S") == self.multi_alarms[key][0] and value[now.weekday()+1] == 1:
  138.                 self.trigger_alarm(key)
  139.                 complete.append(key)
  140.         for k in complete:
  141.             self.next_alarms[k] = self.multi_alarms[k]
  142.             del self.multi_alarms[k]
  143.         if now.strftime("%H:%M:%S") == "00:00:01": #resets the alarms for the next day
  144.             self.multi_alarms = self.next_alarms
  145.  
  146.     def update_clock(self):
  147.         now = datetime.now()
  148.         self.timer.configure(text=now.strftime("%H:%M:%S"))
  149.  
  150.     def run(self): ##main part of the application
  151.         self.root.configure(bg="white") #sets the background to white rather than default gray.
  152.         self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##changes the X (close) Button to run a function instead.
  153.         try:
  154.             self.root.iconbitmap("fav.ico") ##sets the application Icon
  155.         except:
  156.             pass
  157.         self.root.title("MyApp")
  158.         self.root.geometry("800x600")
  159.  
  160.  
  161.         #the current time
  162.         clock = self.tku.cFrame(self.root, fill=X, bg="Blue", side=TOP)
  163.         self.timer = Label(clock, text="00:00:00")
  164.         self.timer.pack(side=TOP, fill=X)
  165.  
  166.         self.tku.cButton(self.root, side=TOP, text="+", command=lambda e=Event(): self.add_alarm_window(e))
  167.  
  168.         #section for alarms to sit
  169.         scrollarea, can2 = self.tku.scrollable_area2(self.root)
  170.         self.alarms = self.tku.cFrame(scrollarea, borderwidth=2, relief="groove", bg="red", fill=BOTH, expand=1, side=TOP)
  171.  
  172.  
  173.         while not self.quit: ##flag to quit the application
  174.             self.root.update_idletasks() #updates the root. same as root.mainloop() but safer and interruptable
  175.             self.root.update() #same as above. This lest you stop the loop or add things to the loop.
  176.             #add extra functions here if you need them to run with the loop#
  177.             self.update_clock()
  178.             self.trigger_alarms()
  179.  
  180.     def quitting(self): ##to set the quit flag
  181.         self.quit = True
  182.  
  183. if __name__ == "__main__":
  184.     app = TKGUI() ##creates instance of GUI class
  185.     try:
  186.         app.run()# starts the application
  187.     except KeyboardInterrupt:
  188.         app.quitting() ##safely quits the application when crtl+C is pressed
  189.     except:
  190.         raise #you can change this to be your own error handler if needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement