Advertisement
AceScottie

alarm_module.py

Jan 18th, 2020
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.17 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 alarm_gui:
  9.     def __init__(self, root, log, tku):
  10.         self.root = root
  11.         self.log = log
  12.         self.tku = tku
  13.         self.quit = True
  14.         self.multi_alarms = {}
  15.         self.next_alarms = {}
  16.  
  17.  
  18.     def add_alarm(self, event, date=[], time=[], days=[], ty=""):
  19.         if ty == "Multi":
  20.             al_time = time[0].get()+":"+time[1].get()+":00"
  21.             d = [0] * len(days)
  22.             for i in range(len(days)):
  23.                 d[i] = days[i].get()
  24.             if len(self.multi_alarms)  < 1:
  25.                 self.multi_alarms[0] = [al_time] + d
  26.             else:
  27.                 self.multi_alarms[max(self.multi_alarms.keys())+1] = [al_time] + d
  28.         self.tku.clear_overlay(Event())
  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.this_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.         al = {}
  41.         tyF = self.tku.cFrame(holder3, side=TOP, fill=X)
  42.         Label(tyF, text="Type", justify=RIGHT, anchor="e", bg="white", borderwidth=1, relief="groove", width=10).pack(side=LEFT)
  43.         al_types = ["Single", "Multi"]
  44.         alV= StringVar(tyF)
  45.         if al_type == None:
  46.             alV.set(None)
  47.         else:
  48.             alV.set(al_type.get())
  49.         al['type'] = OptionMenu(*(tyF, alV) + tuple(al_types))
  50.         al['type'].pack(side=LEFT)
  51.         alV.trace("w", lambda a, b, c, e=Event(), t=alV: self.add_alarm_window(a, b, c, event, t))
  52.         dt_picker = self.tku.cFrame(holder3, fill=BOTH, expand=1)
  53.         if al_type != None:
  54.             if al_type.get() == "Multi":
  55.                 lables = self.tku.cFrame(holder3, fill=X, expand=1)
  56.                 w=4
  57.                 Label(dt_picker, text="Time", borderwidth=1, relief="raised", width=10).pack(side=LEFT)
  58.                 Label(dt_picker, text="M", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  59.                 Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  60.                 Label(dt_picker, text="W", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  61.                 Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  62.                 Label(dt_picker, text="F", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  63.                 Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  64.                 Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  65.                 options = self.tku.cFrame(holder3, fill=X, expand=1)
  66.                 hhV = StringVar()
  67.                 hh = MaxLengthEntry(options, maxlength=2, valtype="int/hour", width=3, textvariable=hhV)
  68.                 hh.pack(side=LEFT)
  69.                 Label(options, text=":", width=3).pack(side=LEFT)
  70.                 mmV = StringVar()
  71.                 mm = MaxLengthEntry(options, maxlength=2, valtype="int/minute", width=3, textvariable=mmV)
  72.                 mm.pack(side=LEFT)
  73.                 ckw = 1
  74.                 Monv =  IntVar()
  75.                 Mon = Checkbutton(options, variable=Monv, width=ckw, borderwidth=1, relief="groove")
  76.                 Mon.pack(side=LEFT)
  77.                 Tuev =  IntVar()
  78.                 Tue = Checkbutton(options, variable=Tuev, width=ckw, borderwidth=1, relief="groove")
  79.                 Tue.pack(side=LEFT)
  80.                 Wedv =  IntVar()
  81.                 Wed = Checkbutton(options, variable=Wedv, width=ckw, borderwidth=1, relief="groove")
  82.                 Wed.pack(side=LEFT)
  83.                 Thuv =  IntVar()
  84.                 Thu = Checkbutton(options, variable=Thuv, width=ckw, borderwidth=1, relief="groove")
  85.                 Thu.pack(side=LEFT)
  86.                 Friv =  IntVar()
  87.                 Fri = Checkbutton(options, variable=Friv, width=ckw, borderwidth=1, relief="groove")
  88.                 Fri.pack(side=LEFT)
  89.                 Satv =  IntVar()
  90.                 Sat = Checkbutton(options, variable=Satv, width=ckw, borderwidth=1, relief="groove")
  91.                 Sat.pack(side=LEFT)
  92.                 Sunv =  IntVar()
  93.                 Sun = Checkbutton(options, variable=Sunv, width=ckw, borderwidth=1, relief="groove")
  94.                 Sun.pack(side=LEFT)
  95.                 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))
  96.  
  97.     def update_alarm(self, event, al_id, date=[], time=[], days=[]):
  98.         al_time = time[0].get()+":"+time[1].get()+":00"
  99.         d = [0] * len(days)
  100.         for i in range(len(days)):
  101.             d[i] = days[i].get()
  102.         self.multi_alarms[al_id] = [al_time] + d
  103.         self.tku.clear_overlay(Event())
  104.         self.list_alarms()
  105.     def delete_alarm(self, event, al_id):
  106.         del self.multi_alarms[al_id]
  107.         self.tku.clear_overlay(Event())
  108.         self.list_alarms()
  109.     def edit_multi_alarm_window(self, event, al_id):
  110.         over = self.tku.overlay(self.this_root, Event(), "Add Alarms", height=200, width=320)
  111.         window = self.tku.cFrame(over, padx=5, pady=5, fill=BOTH, expand=1, bg="white")
  112.         holder = self.tku.cFrame(window, side=TOP, fill=BOTH, expand=1, bg="white")
  113.         can = Canvas(holder, bg="white", highlightthickness=0)
  114.         can.pack(side=TOP, fill=BOTH, expand=1)
  115.         holder2 = self.tku.cFrame(can, fill=X)
  116.         scroll, can2 = self.tku.scrollable_area2(can)
  117.         holder3= self.tku.cFrame(scroll, side=TOP, borderwidth=1, relief="groove")
  118.         alarm = self.multi_alarms[al_id]
  119.         print(alarm)
  120.         dt_picker = self.tku.cFrame(holder3, fill=BOTH, expand=1)
  121.         lables = self.tku.cFrame(holder3, fill=X, expand=1)
  122.         w=4
  123.         Label(dt_picker, text="Time", borderwidth=1, relief="raised", width=10).pack(side=LEFT)
  124.         Label(dt_picker, text="M", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  125.         Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  126.         Label(dt_picker, text="W", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  127.         Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  128.         Label(dt_picker, text="F", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  129.         Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  130.         Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
  131.        
  132.         options = self.tku.cFrame(holder3, fill=X, expand=1)
  133.         hhV = StringVar()
  134.         hh = MaxLengthEntry(options, maxlength=2, valtype="int/hour", width=3, textvariable=hhV)
  135.         hh.pack(side=LEFT)
  136.         hh.insert(0, alarm[0].split(":")[0])
  137.         Label(options, text=":", width=3).pack(side=LEFT)
  138.         mmV = StringVar()
  139.         mm = MaxLengthEntry(options, maxlength=2, valtype="int/minute", width=3, textvariable=mmV)
  140.         mm.pack(side=LEFT)
  141.         mm.insert(0, alarm[0].split(":")[1])
  142.        
  143.         ckw = 1
  144.         Monv =  IntVar()
  145.         Monv.set(alarm[1])
  146.         Mon = Checkbutton(options, variable=Monv, width=ckw, borderwidth=1, relief="groove")
  147.         Mon.pack(side=LEFT)
  148.         Tuev =  IntVar()
  149.         Tuev.set(alarm[2])
  150.         Tue = Checkbutton(options, variable=Tuev, width=ckw, borderwidth=1, relief="groove")
  151.         Tue.pack(side=LEFT)
  152.         Wedv =  IntVar()
  153.         Wedv.set(alarm[3])
  154.         Wed = Checkbutton(options, variable=Wedv, width=ckw, borderwidth=1, relief="groove")
  155.         Wed.pack(side=LEFT)
  156.         Thuv =  IntVar()
  157.         Thuv.set(alarm[4])
  158.         Thu = Checkbutton(options, variable=Thuv, width=ckw, borderwidth=1, relief="groove")
  159.         Thu.pack(side=LEFT)
  160.         Friv =  IntVar()
  161.         Friv.set(alarm[5])
  162.         Fri = Checkbutton(options, variable=Friv, width=ckw, borderwidth=1, relief="groove")
  163.         Fri.pack(side=LEFT)
  164.         Satv =  IntVar()
  165.         Satv.set(alarm[6])
  166.         Sat = Checkbutton(options, variable=Satv, width=ckw, borderwidth=1, relief="groove")
  167.         Sat.pack(side=LEFT)
  168.         Sunv =  IntVar()
  169.         Sunv.set(alarm[7])
  170.         Sun = Checkbutton(options, variable=Sunv, width=ckw, borderwidth=1, relief="groove")
  171.         Sun.pack(side=LEFT)
  172.  
  173.         self.tku.cButton(holder3, text="Save", command=lambda e=Event(), al_id=al_id, t=[hh, mm], day=[Monv, Tuev, Wedv, Thuv, Friv, Satv, Sunv]: self.update_alarm(e, al_id, time=t, days=day))
  174.         self.tku.cButton(holder3, text="Delete", command=lambda e=Event(), al_id=al_id: self.delete_alarm(e, al_id))
  175.  
  176.     def list_alarms(self):
  177.         children = self.alarms.winfo_children()
  178.         for child in children:
  179.             child.pack_forget()
  180.             child.destroy()
  181.         days = [0, "M", "T", "W", "T", "F", "S", "S"]
  182.         holder= self.tku.cFrame(self.alarms, fill=BOTH, expand=1)
  183.         for key, value in self.multi_alarms.items():
  184.             row = self.tku.cFrame(holder, fill=X, side=TOP)
  185.             Label(row, text=value[0]).pack(side=LEFT)
  186.             hol=Frame(row, borderwidth=1, relief="groove")
  187.             hol.pack(side=LEFT)
  188.             for x in range(1, len(value)):
  189.                 if value[x] == 1:
  190.                     Label(hol, text=days[x], fg="blue", bg="white").pack(side=LEFT)
  191.                 else:
  192.                     Label(hol, text=days[x], fg="lightgray", bg="white").pack(side=LEFT)
  193.             self.tku.cButton(row, text="Update", command=lambda e=Event(), al_id=key: self.edit_multi_alarm_window(e, al_id))
  194.            
  195.     def trigger_alarm(self, al_id):
  196.         print("Alarm Triggered for %s at %s" %(int(al_id), self.multi_alarms[al_id][0]))
  197.        
  198.     def trigger_alarms(self):
  199.         alarm_triggered = False
  200.         now = datetime.now()
  201.         complete = []
  202.         for key, value in self.multi_alarms.items():
  203.             if now.strftime("%H:%M:%S") == self.multi_alarms[key][0] and value[now.weekday()+1] == 1:
  204.                 self.trigger_alarm(key)
  205.                 alarm_triggered = True
  206.                 complete.append(key)
  207.         for k in complete:
  208.             self.next_alarms[k] = self.multi_alarms[k]
  209.             del self.multi_alarms[k]
  210.         if now.strftime("%H:%M:%S") == "00:00:01": #resets the alarms for the next day
  211.             self.multi_alarms = self.next_alarms
  212.         return alarm_triggered
  213.  
  214.     def update_clock(self):
  215.         now = datetime.now()
  216.         if not self.quit:
  217.             self.timer.configure(text=now.strftime("%H:%M:%S"))
  218.        
  219.     def main_window(self, holder):
  220.         holder.geometry("800x600")
  221.         self.this_root= holder
  222.         clock = self.tku.cFrame(holder, fill=X, bg="Blue", side=TOP)
  223.         self.timer = Label(clock, text="00:00:00")
  224.         self.timer.pack(side=TOP, fill=X)
  225.         self.tku.cButton(holder, side=TOP, text="+", command=lambda e=Event(): self.add_alarm_window(e))
  226.         #section for alarms to sit
  227.         scrollarea, can2 = self.tku.scrollable_area2(holder)
  228.         self.alarms = self.tku.cFrame(scrollarea, borderwidth=2, relief="groove", bg="white", fill=BOTH, expand=1, side=TOP)
  229.         self.quit = False
  230.  
  231.     def quitting(self): ##to set the quit flag
  232.         self.quit = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement