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 TKGUI:
- def __init__(self):
- self.log = Log("Alarm Clock")
- self.root= Tk() ##initilise the main window
- self.tku = TkUtils(self.log, self.root)
- self.quit = False ##defaults the quit flag
- 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.list_alarms()
- def add_alarm_window(self, a=None, b=None, c=None, event=Event(), al_type=None):
- over = self.tku.overlay(self.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 open_edit(self, event, al_id):
- alarm = self.multi_alarms[al_id]
- print(alarm)
- 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"]
- for key, value in self.multi_alarms.items():
- row = self.tku.cFrame(self.alarms, 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.open_edit(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):
- 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)
- 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
- def update_clock(self):
- now = datetime.now()
- self.timer.configure(text=now.strftime("%H:%M:%S"))
- def run(self): ##main part of the application
- self.root.configure(bg="white") #sets the background to white rather than default gray.
- self.root.protocol("WM_DELETE_WINDOW", self.quitting) ##changes the X (close) Button to run a function instead.
- try:
- self.root.iconbitmap("fav.ico") ##sets the application Icon
- except:
- pass
- self.root.title("MyApp")
- self.root.geometry("800x600")
- #the current time
- clock = self.tku.cFrame(self.root, fill=X, bg="Blue", side=TOP)
- self.timer = Label(clock, text="00:00:00")
- self.timer.pack(side=TOP, fill=X)
- self.tku.cButton(self.root, side=TOP, text="+", command=lambda e=Event(): self.add_alarm_window(e))
- #section for alarms to sit
- scrollarea, can2 = self.tku.scrollable_area2(self.root)
- self.alarms = self.tku.cFrame(scrollarea, borderwidth=2, relief="groove", bg="red", fill=BOTH, expand=1, side=TOP)
- while not self.quit: ##flag to quit the application
- self.root.update_idletasks() #updates the root. same as root.mainloop() but safer and interruptable
- self.root.update() #same as above. This lest you stop the loop or add things to the loop.
- #add extra functions here if you need them to run with the loop#
- self.update_clock()
- self.trigger_alarms()
- def quitting(self): ##to set the quit flag
- self.quit = True
- if __name__ == "__main__":
- app = TKGUI() ##creates instance of GUI class
- try:
- app.run()# starts the application
- except KeyboardInterrupt:
- app.quitting() ##safely quits the application when crtl+C is pressed
- except:
- raise #you can change this to be your own error handler if needed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement