Sign_In_Register/index.py

87 lines
3.2 KiB
Python

from tkinter import *
from tkinter import ttk
import pandas as pd
from employee_manifest import EmployeeManifest
from home import Home
from sign_in import SignIn
import sched
import time
import os
from datetime import datetime, date , timedelta
from threading import Thread
from queue import Queue
#Special mention to James Haywood for imaging the PI $$ <3 <3!
class Main(Tk):
def __init__(self):
Tk.__init__(self)
if os.name == 'nt':
self.geometry('520x450')
else:
self.geometry('720x450')
self.winfo_toplevel().title("Sign in App")
self.main_frame = Frame(self)
self.main_frame.pack(side='top', fill=BOTH, expand=1, pady=10)
#Thread safe queue and event to check queue for scheduled sign out at midnight
#Required because scheduler is run in seperate thread which causes tkinter to break without
self.sched_queue = Queue()
self.bind('<<CheckQueue>>', self.run_signout)
#List of GUI's used for mounting on button click
class FramesList():
home = Home
employee_manifest = EmployeeManifest
sign_in = SignIn
self.frames = FramesList()
#Mount home frame when class envoked (program started)
self.current_frame = self.frames.home(self.main_frame, self)
self.current_frame.place(anchor="c", relx=.5, rely=.5)
self.home_btn = Button(text = 'Home', command=lambda: Main.frame_update(self, 'home'))
self.home_btn.place(anchor='w', x=5, y=20)
self.sched_thread = Thread(target=self.run_sched, daemon=True)
self.sched_thread.start()
#Destroy the current GUI frame and mount a requested GUI
def frame_update(self, requested_frame):
self.current_frame.destroy()
next_frame = getattr(self.frames, requested_frame)
self.current_frame = next_frame(self.main_frame, self)
self.current_frame.place(anchor="c", relx=.5, rely=.5)
#Add function to the thread safe queue and then generate an event that causes the queue to be checked
def update_queue(event, self):
self.sched_queue.put(self.clear_all_signin)
self.event_generate('<<CheckQueue>>')
#When CheckQueue event is fired this function is run
#Gets function from queue and then runs it
def run_signout(self, event):
msg = self.sched_queue.get()
msg()
#Clears sign ins (scheduled to run at the start of next day) by just destroying and re-mounting the sign in frame
def clear_all_signin(self):
print('clear sign in executed')
if type(app.current_frame).__name__ == 'SignIn':
print('p0ng')
self.frame_update('sign_in')
self.run_sched()
#Rune scheduled task at 12am each day
def run_sched(self):
scheduler = sched.scheduler(time.time, time.sleep)
tomorrows_date = date.today() + timedelta(1)
#todays_date = date.today()
scheduled_time = datetime(tomorrows_date.year, tomorrows_date.month, tomorrows_date.day, 00, 00, 00)
scheduler.enterabs(scheduled_time.timestamp(), 1, self.clear_all_signin)
scheduler.run()
app = Main()
app.mainloop()