Advertisement
Divyansh_Chourey

Library_Managment

Feb 15th, 2021 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.07 KB | None | 0 0
  1. '''
  2. -------- Library Managment System --------
  3. This is a Library managment GUI
  4. Author: Divyansh Chourey
  5.  
  6. '''
  7. #importing libraries
  8. from tkinter import *
  9. from tkinter import messagebox
  10. import os
  11.  
  12. '''-------- Creating class --------'''
  13. #Creating Library class
  14. class Library_gui(Tk):
  15.     def __init__(self, book_list):
  16.         super().__init__()
  17.         self.geometry("1365x635")
  18.         self.title("Library Managment")
  19.         self.book_list=book_list
  20.    
  21.     #Heading of first page
  22.     def head_label(self):
  23.         lab=Label(self, text="Central Library", font="arial 16 bold", pady=40)
  24.         lab.pack()
  25.    
  26.     '''------ Buttons to be placed ------'''
  27.     def list_books_btn(self, func):
  28.         btn=Button(self,text="List Book", command=func, padx=57, borderwidth=6, relief=RAISED)
  29.         btn.pack()
  30.        
  31.     def borrow_btn(self, func):
  32.         btn=Button(self,text="Borrow Book", command=func, padx=30, borderwidth=6, relief=RAISED)
  33.         btn.pack()
  34.        
  35.     def return_book_btn(self, func):
  36.         btn=Button(self,text="Return Book", command=func, padx=36, borderwidth=6, relief=RAISED)
  37.         btn.pack()
  38.        
  39.     def exit_btn(self):
  40.         btn=Button(self,text="Exit", command=quit, padx=105, borderwidth=6, relief=RAISED)
  41.         btn.pack()
  42.        
  43.     def about_btn(self):
  44.         def about_func():
  45.             messagebox.showinfo("About", "This is a Library managment GUI\nCreated by Divyansh Chourey")
  46.         btn=Button(self, text="About", command=about_func, padx=87, borderwidth=6, relief=RAISED)
  47.         btn.pack()
  48.        
  49.     '''------- Functions for button. --------'''
  50.     #Function for listing all the books
  51.     def list_books_func(self):
  52.         win_book=Toplevel()
  53.         win_book.title("List of avalible books.")
  54.         win_book.geometry("1365x635")
  55.        
  56.         Label(win_book, text="The avalible books are:\n", font="arial 10 italic").pack()
  57.         for bookList in self.book_list:
  58.             Label(win_book, text=bookList).pack()
  59.        
  60.         back=Button(win_book, text="Back", command=win_book.destroy, borderwidth=6, relief=RAISED)
  61.         back.pack(side=BOTTOM, anchor='sw')
  62.        
  63.         win_book.mainloop()
  64.    
  65.     #Function for borrow button
  66.     def borrow_func(self):
  67.         win_borrow=Toplevel()
  68.         win_borrow.title("Borrow a book.")
  69.         win_borrow.geometry("1365x635")
  70.        
  71.         user=Entry(win_borrow, font="arial 10 italic")
  72.         user.pack()
  73.        
  74.         def func_borrow():
  75.             if user.get().title() in self.book_list:
  76.                 messagebox.showinfo("Borrowing....", f"You have been issued {user.get().title()}")
  77.                 self.book_list.remove(user.get().title())
  78.                 with open('books.txt', 'r') as f:
  79.                     lines=f.readlines()
  80.                 with open ('books.txt', 'w') as f:
  81.                     for line in lines:
  82.                         if line.strip("\n") != user.get().title():
  83.                             f.write(line)
  84.  
  85.             else:
  86.                 messagebox.showinfo("Borrowing....", f"{user.get()} is not avalible.")
  87.  
  88.            
  89.         btn_borrow=Button(win_borrow, text="Borrow book", command=func_borrow, borderwidth=6, relief=RAISED)
  90.         btn_borrow.pack()
  91.        
  92.         back=Button(win_borrow, text="Back", command=win_borrow.destroy, borderwidth=6, relief=RAISED)
  93.         back.pack(side=BOTTOM, anchor='sw')
  94.        
  95.         win_borrow.mainloop()
  96.    
  97.     #Function for return button
  98.     def return_func(self):
  99.         win_return=Toplevel()
  100.         win_return.title("Returning book.")
  101.         win_return.geometry("1365x635")
  102.        
  103.         user=Entry(win_return, font="arial 10 italic")
  104.         user.pack()
  105.        
  106.         def func_return():
  107.             self.book_list.append(user.get().title())
  108.             messagebox.showinfo("Returning......", f"Thanks for returning/donating\n {user.get().title()}")
  109.             with open ("books.txt", 'a') as f:
  110.                 f.write("\n"+user.get().title())
  111.        
  112.         btn_return=Button(win_return, text="Return", command=func_return, borderwidth=6, relief=RAISED)
  113.         btn_return.pack()
  114.        
  115.         back=Button(win_return, text="Back", command=win_return.destroy, borderwidth=6, relief=RAISED)
  116.         back.pack(side=BOTTOM, anchor='sw')
  117.        
  118.         win_return.mainloop()
  119.    
  120. if __name__=='__main__':
  121.     #In case when books.txt not found
  122.     write_books='''
  123.    Python Notes
  124.    Theory of everything
  125.    Out Look
  126.    A Brief History of Time
  127.    '''
  128.     if (not os.path.exists("books.txt")):
  129.         with open("books.txt", 'w') as f:
  130.             f.write(write_books)
  131.     #Opening books.txt file
  132.     with open ("books.txt", 'r') as f:
  133.         content=f.read()
  134.         content=content.split("\n")
  135.        
  136.     #Creating GUI
  137.     window=Library_gui(content)
  138.    
  139.     window.head_label()
  140.     window.list_books_btn(window.list_books_func)
  141.     window.borrow_btn(window.borrow_func)
  142.     window.return_book_btn(window.return_func)
  143.     window.about_btn()
  144.     window.exit_btn()
  145.    
  146.     window.mainloop()
  147.    
  148.  
  149.  
  150.  
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement