Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from datetime import datetime, timedelta
- ##custom imports
- from aceutil import TkUtils, Log #download from https://pastebin.com/yUaMxmv3
- from tkutils import * #download from https://pastebin.com/TLdsJ2er
- class alarm_gui:
- def __init__(self, root, log, tku):
- self.root = root
- self.log = log
- self.tku = tku
- self.quit = True
- self.multi_alarms = {}
- self.next_alarms = {}
- def add_alarm(self, event, date=[], time=[], days=[], ty=""):
- if ty == "Multi":
- al_time = time[0].get()+":"+time[1].get()+":00"
- d = [0] * len(days)
- for i in range(len(days)):
- d[i] = days[i].get()
- if len(self.multi_alarms) < 1:
- self.multi_alarms[0] = [al_time] + d
- else:
- self.multi_alarms[max(self.multi_alarms.keys())+1] = [al_time] + d
- self.tku.clear_overlay(Event())
- self.list_alarms()
- def add_alarm_window(self, a=None, b=None, c=None, event=Event(), al_type=None):
- over = self.tku.overlay(self.this_root, Event(), "Add Alarms", height=200, width=320)
- window = self.tku.cFrame(over, padx=5, pady=5, fill=BOTH, expand=1, bg="white")
- holder = self.tku.cFrame(window, side=TOP, fill=BOTH, expand=1, bg="white")
- can = Canvas(holder, bg="white", highlightthickness=0)
- can.pack(side=TOP, fill=BOTH, expand=1)
- holder2 = self.tku.cFrame(can, fill=X)
- scroll, can2 = self.tku.scrollable_area2(can)
- holder3= self.tku.cFrame(scroll, side=TOP, borderwidth=1, relief="groove")
- al = {}
- tyF = self.tku.cFrame(holder3, side=TOP, fill=X)
- Label(tyF, text="Type", justify=RIGHT, anchor="e", bg="white", borderwidth=1, relief="groove", width=10).pack(side=LEFT)
- al_types = ["Single", "Multi"]
- alV= StringVar(tyF)
- if al_type == None:
- alV.set(None)
- else:
- alV.set(al_type.get())
- al['type'] = OptionMenu(*(tyF, alV) + tuple(al_types))
- al['type'].pack(side=LEFT)
- alV.trace("w", lambda a, b, c, e=Event(), t=alV: self.add_alarm_window(a, b, c, event, t))
- dt_picker = self.tku.cFrame(holder3, fill=BOTH, expand=1)
- if al_type != None:
- if al_type.get() == "Multi":
- lables = self.tku.cFrame(holder3, fill=X, expand=1)
- w=4
- Label(dt_picker, text="Time", borderwidth=1, relief="raised", width=10).pack(side=LEFT)
- Label(dt_picker, text="M", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="W", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="F", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- options = self.tku.cFrame(holder3, fill=X, expand=1)
- hhV = StringVar()
- hh = MaxLengthEntry(options, maxlength=2, valtype="int/hour", width=3, textvariable=hhV)
- hh.pack(side=LEFT)
- Label(options, text=":", width=3).pack(side=LEFT)
- mmV = StringVar()
- mm = MaxLengthEntry(options, maxlength=2, valtype="int/minute", width=3, textvariable=mmV)
- mm.pack(side=LEFT)
- ckw = 1
- Monv = IntVar()
- Mon = Checkbutton(options, variable=Monv, width=ckw, borderwidth=1, relief="groove")
- Mon.pack(side=LEFT)
- Tuev = IntVar()
- Tue = Checkbutton(options, variable=Tuev, width=ckw, borderwidth=1, relief="groove")
- Tue.pack(side=LEFT)
- Wedv = IntVar()
- Wed = Checkbutton(options, variable=Wedv, width=ckw, borderwidth=1, relief="groove")
- Wed.pack(side=LEFT)
- Thuv = IntVar()
- Thu = Checkbutton(options, variable=Thuv, width=ckw, borderwidth=1, relief="groove")
- Thu.pack(side=LEFT)
- Friv = IntVar()
- Fri = Checkbutton(options, variable=Friv, width=ckw, borderwidth=1, relief="groove")
- Fri.pack(side=LEFT)
- Satv = IntVar()
- Sat = Checkbutton(options, variable=Satv, width=ckw, borderwidth=1, relief="groove")
- Sat.pack(side=LEFT)
- Sunv = IntVar()
- Sun = Checkbutton(options, variable=Sunv, width=ckw, borderwidth=1, relief="groove")
- Sun.pack(side=LEFT)
- 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))
- def update_alarm(self, event, al_id, date=[], time=[], days=[]):
- al_time = time[0].get()+":"+time[1].get()+":00"
- d = [0] * len(days)
- for i in range(len(days)):
- d[i] = days[i].get()
- self.multi_alarms[al_id] = [al_time] + d
- self.tku.clear_overlay(Event())
- self.list_alarms()
- def delete_alarm(self, event, al_id):
- del self.multi_alarms[al_id]
- self.tku.clear_overlay(Event())
- self.list_alarms()
- def edit_multi_alarm_window(self, event, al_id):
- over = self.tku.overlay(self.this_root, Event(), "Add Alarms", height=200, width=320)
- window = self.tku.cFrame(over, padx=5, pady=5, fill=BOTH, expand=1, bg="white")
- holder = self.tku.cFrame(window, side=TOP, fill=BOTH, expand=1, bg="white")
- can = Canvas(holder, bg="white", highlightthickness=0)
- can.pack(side=TOP, fill=BOTH, expand=1)
- holder2 = self.tku.cFrame(can, fill=X)
- scroll, can2 = self.tku.scrollable_area2(can)
- holder3= self.tku.cFrame(scroll, side=TOP, borderwidth=1, relief="groove")
- alarm = self.multi_alarms[al_id]
- print(alarm)
- dt_picker = self.tku.cFrame(holder3, fill=BOTH, expand=1)
- lables = self.tku.cFrame(holder3, fill=X, expand=1)
- w=4
- Label(dt_picker, text="Time", borderwidth=1, relief="raised", width=10).pack(side=LEFT)
- Label(dt_picker, text="M", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="W", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="T", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="F", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- Label(dt_picker, text="S", borderwidth=1, relief="raised", width=w).pack(side=LEFT)
- options = self.tku.cFrame(holder3, fill=X, expand=1)
- hhV = StringVar()
- hh = MaxLengthEntry(options, maxlength=2, valtype="int/hour", width=3, textvariable=hhV)
- hh.pack(side=LEFT)
- hh.insert(0, alarm[0].split(":")[0])
- Label(options, text=":", width=3).pack(side=LEFT)
- mmV = StringVar()
- mm = MaxLengthEntry(options, maxlength=2, valtype="int/minute", width=3, textvariable=mmV)
- mm.pack(side=LEFT)
- mm.insert(0, alarm[0].split(":")[1])
- ckw = 1
- Monv = IntVar()
- Monv.set(alarm[1])
- Mon = Checkbutton(options, variable=Monv, width=ckw, borderwidth=1, relief="groove")
- Mon.pack(side=LEFT)
- Tuev = IntVar()
- Tuev.set(alarm[2])
- Tue = Checkbutton(options, variable=Tuev, width=ckw, borderwidth=1, relief="groove")
- Tue.pack(side=LEFT)
- Wedv = IntVar()
- Wedv.set(alarm[3])
- Wed = Checkbutton(options, variable=Wedv, width=ckw, borderwidth=1, relief="groove")
- Wed.pack(side=LEFT)
- Thuv = IntVar()
- Thuv.set(alarm[4])
- Thu = Checkbutton(options, variable=Thuv, width=ckw, borderwidth=1, relief="groove")
- Thu.pack(side=LEFT)
- Friv = IntVar()
- Friv.set(alarm[5])
- Fri = Checkbutton(options, variable=Friv, width=ckw, borderwidth=1, relief="groove")
- Fri.pack(side=LEFT)
- Satv = IntVar()
- Satv.set(alarm[6])
- Sat = Checkbutton(options, variable=Satv, width=ckw, borderwidth=1, relief="groove")
- Sat.pack(side=LEFT)
- Sunv = IntVar()
- Sunv.set(alarm[7])
- Sun = Checkbutton(options, variable=Sunv, width=ckw, borderwidth=1, relief="groove")
- Sun.pack(side=LEFT)
- 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))
- self.tku.cButton(holder3, text="Delete", command=lambda e=Event(), al_id=al_id: self.delete_alarm(e, al_id))
- def list_alarms(self):
- children = self.alarms.winfo_children()
- for child in children:
- child.pack_forget()
- child.destroy()
- days = [0, "M", "T", "W", "T", "F", "S", "S"]
- holder= self.tku.cFrame(self.alarms, fill=BOTH, expand=1)
- for key, value in self.multi_alarms.items():
- row = self.tku.cFrame(holder, fill=X, side=TOP)
- Label(row, text=value[0]).pack(side=LEFT)
- hol=Frame(row, borderwidth=1, relief="groove")
- hol.pack(side=LEFT)
- for x in range(1, len(value)):
- if value[x] == 1:
- Label(hol, text=days[x], fg="blue", bg="white").pack(side=LEFT)
- else:
- Label(hol, text=days[x], fg="lightgray", bg="white").pack(side=LEFT)
- self.tku.cButton(row, text="Update", command=lambda e=Event(), al_id=key: self.edit_multi_alarm_window(e, al_id))
- def trigger_alarm(self, al_id):
- print("Alarm Triggered for %s at %s" %(int(al_id), self.multi_alarms[al_id][0]))
- def trigger_alarms(self):
- alarm_triggered = False
- now = datetime.now()
- complete = []
- for key, value in self.multi_alarms.items():
- if now.strftime("%H:%M:%S") == self.multi_alarms[key][0] and value[now.weekday()+1] == 1:
- self.trigger_alarm(key)
- alarm_triggered = True
- complete.append(key)
- for k in complete:
- self.next_alarms[k] = self.multi_alarms[k]
- del self.multi_alarms[k]
- if now.strftime("%H:%M:%S") == "00:00:01": #resets the alarms for the next day
- self.multi_alarms = self.next_alarms
- return alarm_triggered
- def update_clock(self):
- now = datetime.now()
- if not self.quit:
- self.timer.configure(text=now.strftime("%H:%M:%S"))
- def main_window(self, holder):
- holder.geometry("800x600")
- self.this_root= holder
- clock = self.tku.cFrame(holder, fill=X, bg="Blue", side=TOP)
- self.timer = Label(clock, text="00:00:00")
- self.timer.pack(side=TOP, fill=X)
- self.tku.cButton(holder, side=TOP, text="+", command=lambda e=Event(): self.add_alarm_window(e))
- #section for alarms to sit
- scrollarea, can2 = self.tku.scrollable_area2(holder)
- self.alarms = self.tku.cFrame(scrollarea, borderwidth=2, relief="groove", bg="white", fill=BOTH, expand=1, side=TOP)
- self.quit = False
- def quitting(self): ##to set the quit flag
- self.quit = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement