import Tkinter as Tk
from functools import partial
from PIL import ImageTk,Image
import Inventory
########################################################################
class MyApp(object):
    def __init__(self, parent):
        """Constructor"""
        self.root = parent
        self.root.title("Main frame")
        self.frame = Tk.Frame(parent,bg="#000000",bd=5)
        self.frame.place(relx=0.20,rely=0.25,relwidth=0.6,relheight=0.5)
	Label1 =Tk.Label(self.frame,text="Welcome to our Library",fg='white',font=('Times New Roman',30,"bold italic"),bg='black')
	Label1.pack()
	button1=Tk.Button(self.frame,text="Staff",bg='white', fg='black',command=self.Staffpw)
	button1.place(relx=0.28,rely=0.25,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(self.frame,text="Student",bg='white', fg='black',command=self.Student)
	button2.place(relx=0.28,rely=0.4,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(self.frame,text="Exit",bg='white', fg='black',command=self.root.destroy)
	button3.place(relx=0.28,rely=0.55,relwidth=0.45,relheight=0.15)
	
    def onCloseOtherFrame(self, otherFrame):
        otherFrame.destroy()
        self.root.deiconify()

    def exitbuttonclick(self,curr,prev):
	curr.destroy()
	prev.deiconify()
    
    def Deletion(self,frame):
	frame.withdraw()
	DelFrame = Tk.Toplevel(bg="#000000",bd=5)
        DelFrame.geometry("800x800")
        DelFrame.title("Deletions")
	button1=Tk.Button(DelFrame,text="Deleting a Student",bg='black', fg='white',command=lambda: Inventory.deletestudents(DelFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(DelFrame,text="Deleting a Book",bg='black', fg='white',command=lambda: Inventory.delbooks(DelFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(DelFrame,text="Deleting an Author",bg='black', fg='white',command=lambda: Inventory.delauth(DelFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
	button4=Tk.Button(DelFrame,text="Deleting a Genre",bg='black', fg='white',command=lambda: Inventory.delgen(DelFrame,self.root))
	button4.place(relx=0.28,rely=0.5,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(DelFrame, text="Exit", command=lambda: self.exitbuttonclick(DelFrame,frame))
	btn.place(relx=0.48,rely=0.78)
	DelFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def Upmod(self,frame):
	frame.withdraw()
	UpFrame = Tk.Toplevel(bg="#000000",bd=5)
        UpFrame.geometry("800x800")
        UpFrame.title("Updates and Modifications")
	button1=Tk.Button(UpFrame,text="Modify Student Info",bg='black', fg='white',command=lambda: Inventory.updates(UpFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(UpFrame,text="Modify Author Info",bg='black', fg='white',command=lambda: Inventory.updatea(UpFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(UpFrame,text="Modify Book Info",bg='black', fg='white',command=lambda: Inventory.updateb(UpFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
	button4=Tk.Button(UpFrame,text="Restock Books",bg='black', fg='white',command=lambda: Inventory.replenish(UpFrame,self.root))
	button4.place(relx=0.28,rely=0.5,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(UpFrame, text="Exit", command=lambda: self.exitbuttonclick(UpFrame,frame))
	btn.place(relx=0.48,rely=0.78)
	UpFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def FinEn(self,frame):
	frame.withdraw()
	FinFrame = Tk.Toplevel(bg="#000000",bd=5)
        FinFrame.geometry("800x800")
        FinFrame.title("Fines, Enquiries and Comments")
	button1=Tk.Button(FinFrame,text="View and Delete Enquiries/Comments",bg='black', fg='white',command=lambda: Inventory.viewenqco(FinFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(FinFrame,text="View and Pay Fines",bg='black', fg='white',command=lambda: Inventory.finepaid(FinFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(FinFrame, text="Exit", command=lambda: self.exitbuttonclick(FinFrame,frame))
	btn.place(relx=0.48,rely=0.48)
	FinFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def Display(self,frame):
	frame.withdraw()
	DisFrame = Tk.Toplevel(bg="#000000",bd=5)
        DisFrame.geometry("800x800")
        DisFrame.title("Display")
	button1=Tk.Button(DisFrame,text="Display All Students",bg='black', fg='white',command=lambda: Inventory.displays(DisFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(DisFrame,text="Display Students department wise",bg='black', fg='white',command=lambda: Inventory.displaysc(DisFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(DisFrame,text="Display Personal Information of Students",bg='black', fg='white',command=lambda: Inventory.memberinfo(DisFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(DisFrame, text="Exit", command=lambda: self.exitbuttonclick(DisFrame,frame))
	btn.place(relx=0.48,rely=0.58)
	DisFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def Registrations(self,frame):
	frame.withdraw()
	RegFrame = Tk.Toplevel(bg="#000000",bd=5)
        RegFrame.geometry("800x800")
        RegFrame.title("Registrations")
	button1=Tk.Button(RegFrame,text="Register New books",bg='black', fg='white',command=lambda: Inventory.addbooks(RegFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(RegFrame,text="Register New Students",bg='black', fg='white',command=lambda: Inventory.registerstudents(RegFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(RegFrame,text="Register New Authors",bg='black', fg='white',command=lambda: Inventory.addauth(RegFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
	button4=Tk.Button(RegFrame,text="Register Genre Categories",bg='black', fg='white',command=lambda: Inventory.addgencat(RegFrame,self.root))
	button4.place(relx=0.28,rely=0.5,relwidth=0.45,relheight=0.15)
	button5=Tk.Button(RegFrame,text="Register Books with Genres",bg='black', fg='white',command=lambda: Inventory.addgenre(RegFrame,self.root))
	button5.place(relx=0.28,rely=0.65,relwidth=0.45,relheight=0.15)
	button6=Tk.Button(RegFrame,text="Register Personal Information of Students",bg='black', fg='white',command=lambda: Inventory.addmemberinfo(RegFrame,self.root))
	button6.place(relx=0.28,rely=0.8,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(RegFrame, text="Exit", command=lambda: self.exitbuttonclick(RegFrame,frame))
	btn.place(relx=0.48,rely=0.98)
	RegFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def Staff(self):
	StaffFrame = Tk.Toplevel(bg="#000000",bd=5)
        StaffFrame.geometry("800x800")
        StaffFrame.title("Staff")
	button1=Tk.Button(StaffFrame,text="Registrations",bg='black', fg='white',command=lambda: self.Registrations(StaffFrame))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(StaffFrame,text="Display",bg='black', fg='white',command=lambda: self.Display(StaffFrame))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(StaffFrame,text="Deletion",bg='black', fg='white',command=lambda: self.Deletion(StaffFrame))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
	button4=Tk.Button(StaffFrame,text="Updates and modifications",bg='black', fg='white',command=lambda: self.Upmod(StaffFrame))
	button4.place(relx=0.28,rely=0.5,relwidth=0.45,relheight=0.15)
	button5=Tk.Button(StaffFrame,text="Fines, Enquiries and Comments",bg='black', fg='white',command=lambda: self.FinEn(StaffFrame))
	button5.place(relx=0.28,rely=0.65,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(StaffFrame, text="Exit", command=lambda: self.onCloseOtherFrame(StaffFrame))
	btn.place(relx=0.48,rely=0.88)
	StaffFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)
    
    def Sdisplay(self,frame):
	frame.withdraw()
	SDisFrame = Tk.Toplevel(bg="#000000",bd=5)
        SDisFrame.geometry("800x800")
        SDisFrame.title("Display")
	button1=Tk.Button(SDisFrame,text="Display All Books",bg='black', fg='white',command=lambda: Inventory.displayb(SDisFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(SDisFrame,text="Display All Authors",bg='black', fg='white',command=lambda: Inventory.viewa(SDisFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(SDisFrame,text="Display books by genre listing",bg='black', fg='white',command=lambda: Inventory.viewbg(SDisFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
	button4=Tk.Button(SDisFrame,text="Display books by author listing",bg='black', fg='white',command=lambda: Inventory.viewba(SDisFrame,self.root))
	button4.place(relx=0.28,rely=0.5,relwidth=0.45,relheight=0.15)
	button5=Tk.Button(SDisFrame,text="Display borrowed books",bg='black', fg='white',command=lambda: Inventory.viewlog1(SDisFrame,self.root))
	button5.place(relx=0.28,rely=0.65,relwidth=0.45,relheight=0.15)
	button6=Tk.Button(SDisFrame,text="Display Fines",bg='black', fg='white',command=lambda: Inventory.viewf(SDisFrame,self.root))
	button6.place(relx=0.28,rely=0.8,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(SDisFrame, text="Exit", command=lambda: self.exitbuttonclick(SDisFrame,frame))
	btn.place(relx=0.48,rely=0.98)
	SDisFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def IRL(self,frame):
	frame.withdraw()
	IRLFrame = Tk.Toplevel(bg="#000000",bd=5)
        IRLFrame.geometry("800x800")
        IRLFrame.title("Issues/Return/Lost")
	button1=Tk.Button(IRLFrame,text="Issue a Book",bg='black', fg='white',command=lambda: Inventory.issueb(IRLFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(IRLFrame,text="Return a Book",bg='black', fg='white',command=lambda: Inventory.returnb(IRLFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(IRLFrame,text="Report a Lost Book",bg='black', fg='white',command=lambda: Inventory.lostb(IRLFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(IRLFrame, text="Exit", command=lambda: self.exitbuttonclick(IRLFrame,frame))
	btn.place(relx=0.48,rely=0.58)
	IRLFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def Search(self,frame):
	frame.withdraw()
	SFrame = Tk.Toplevel(bg="#000000",bd=5)
        SFrame.geometry("800x800")
        SFrame.title("Search")
	button1=Tk.Button(SFrame,text="Search by Author",bg='black', fg='white',command=lambda: Inventory.searcha(SFrame,self.root))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(SFrame,text="Search by Genre",bg='black', fg='white',command=lambda: Inventory.searchg(SFrame,self.root))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(SFrame,text="Search by Title",bg='black', fg='white',command=lambda: Inventory.searcht(SFrame,self.root))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
        btn = Tk.Button(SFrame, text="Exit", command=lambda: self.exitbuttonclick(SFrame,frame))
	btn.place(relx=0.48,rely=0.58)
	SFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)
    
    def Student(self):
	self.root.withdraw()
	StudentFrame = Tk.Toplevel(bg="#000000",bd=5)
        StudentFrame.geometry("800x800")
        StudentFrame.title("Student")
	button1=Tk.Button(StudentFrame,text="Display",bg='black', fg='white',command=lambda: self.Sdisplay(StudentFrame))
	button1.place(relx=0.28,rely=0.05,relwidth=0.45,relheight=0.15)
	button2=Tk.Button(StudentFrame,text="Issue/Return/Lost",bg='black', fg='white',command=lambda: self.IRL(StudentFrame))
	button2.place(relx=0.28,rely=0.20,relwidth=0.45,relheight=0.15)
	button3=Tk.Button(StudentFrame,text="Search",bg='black', fg='white',command=lambda: self.Search(StudentFrame))
	button3.place(relx=0.28,rely=0.35,relwidth=0.45,relheight=0.15)
	button4=Tk.Button(StudentFrame,text="Enquiries and Comments",bg='black', fg='white',command=lambda: Inventory.addenqco(StudentFrame,self.root))
	button4.place(relx=0.28,rely=0.5,relwidth=0.45,relheight=0.15)
        handler = lambda: self.onCloseOtherFrame(StudentFrame)
        btn = Tk.Button(StudentFrame, text="Exit", command=handler)
        btn.place(relx=0.48,rely=0.78)
	StudentFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)

    def validate(self,p,otherframe):
	if p.get()=='17':
		otherframe.destroy()
		self.Staff()
	else:
		self.onCloseOtherFrame(otherframe)
	
    def Staffpw(self):
	self.root.withdraw()
	StaffpwFrame = Tk.Toplevel(bg="#000000",bd=5)
        StaffpwFrame.geometry("800x800")
        StaffpwFrame.title("Staff Authentication")
	Label1 = Tk.Label(StaffpwFrame,text="Please enter password to authenticate",font=('Times New Roman',20,"bold italic"),bg='black',fg='white')
	Label1.place(relx=0.24,rely=0.35)
	p=Tk.StringVar()
	Entry1 = Tk.Entry(StaffpwFrame,textvariable=p,show='*')
	Entry1.place(relx=0.24,rely=0.45,relwidth=0.25,relheight=0.05)
	handler = lambda: self.validate(p,StaffpwFrame)
	btn = Tk.Button(StaffpwFrame, text="Authenticate", command=handler)
	btn.place(relx=0.24,rely=0.55,relwidth=0.15,relheight=0.05)
	btn = Tk.Button(StaffpwFrame, text="Back", command=lambda: self.onCloseOtherFrame(StaffpwFrame))
	btn.place(relx=0.4,rely=0.55,relwidth=0.15,relheight=0.05)
	StaffpwFrame.protocol("WM_DELETE_WINDOW", self.root.destroy)
same=True
n=0.5
root = Tk.Tk()
root.geometry("800x800")
canvas=Tk.Canvas(root,width=100,height=160)
image1=Image.open("Bg.ppm")
C = Tk.Canvas(root, bg="blue", height=250, width=300)
background_image = image1.resize((800,800),Image.ANTIALIAS)
image=ImageTk.PhotoImage(background_image)
background_label = Tk.Label(root, image=image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
app = MyApp(root)
root.mainloop()
