Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- -------- Library Managment System --------
- This is a Library managment GUI
- Author: Divyansh Chourey
- '''
- #importing libraries
- from tkinter import *
- from tkinter import messagebox
- import os
- '''-------- Creating class --------'''
- #Creating Library class
- class Library_gui(Tk):
- def __init__(self, book_list):
- super().__init__()
- self.geometry("1365x635")
- self.title("Library Managment")
- self.book_list=book_list
- #Heading of first page
- def head_label(self):
- lab=Label(self, text="Central Library", font="arial 16 bold", pady=40)
- lab.pack()
- '''------ Buttons to be placed ------'''
- def list_books_btn(self, func):
- btn=Button(self,text="List Book", command=func, padx=57, borderwidth=6, relief=RAISED)
- btn.pack()
- def borrow_btn(self, func):
- btn=Button(self,text="Borrow Book", command=func, padx=30, borderwidth=6, relief=RAISED)
- btn.pack()
- def return_book_btn(self, func):
- btn=Button(self,text="Return Book", command=func, padx=36, borderwidth=6, relief=RAISED)
- btn.pack()
- def exit_btn(self):
- btn=Button(self,text="Exit", command=quit, padx=105, borderwidth=6, relief=RAISED)
- btn.pack()
- def about_btn(self):
- def about_func():
- messagebox.showinfo("About", "This is a Library managment GUI\nCreated by Divyansh Chourey")
- btn=Button(self, text="About", command=about_func, padx=87, borderwidth=6, relief=RAISED)
- btn.pack()
- '''------- Functions for button. --------'''
- #Function for listing all the books
- def list_books_func(self):
- win_book=Toplevel()
- win_book.title("List of avalible books.")
- win_book.geometry("1365x635")
- Label(win_book, text="The avalible books are:\n", font="arial 10 italic").pack()
- for bookList in self.book_list:
- Label(win_book, text=bookList).pack()
- back=Button(win_book, text="Back", command=win_book.destroy, borderwidth=6, relief=RAISED)
- back.pack(side=BOTTOM, anchor='sw')
- win_book.mainloop()
- #Function for borrow button
- def borrow_func(self):
- win_borrow=Toplevel()
- win_borrow.title("Borrow a book.")
- win_borrow.geometry("1365x635")
- user=Entry(win_borrow, font="arial 10 italic")
- user.pack()
- def func_borrow():
- if user.get().title() in self.book_list:
- messagebox.showinfo("Borrowing....", f"You have been issued {user.get().title()}")
- self.book_list.remove(user.get().title())
- with open('books.txt', 'r') as f:
- lines=f.readlines()
- with open ('books.txt', 'w') as f:
- for line in lines:
- if line.strip("\n") != user.get().title():
- f.write(line)
- else:
- messagebox.showinfo("Borrowing....", f"{user.get()} is not avalible.")
- btn_borrow=Button(win_borrow, text="Borrow book", command=func_borrow, borderwidth=6, relief=RAISED)
- btn_borrow.pack()
- back=Button(win_borrow, text="Back", command=win_borrow.destroy, borderwidth=6, relief=RAISED)
- back.pack(side=BOTTOM, anchor='sw')
- win_borrow.mainloop()
- #Function for return button
- def return_func(self):
- win_return=Toplevel()
- win_return.title("Returning book.")
- win_return.geometry("1365x635")
- user=Entry(win_return, font="arial 10 italic")
- user.pack()
- def func_return():
- self.book_list.append(user.get().title())
- messagebox.showinfo("Returning......", f"Thanks for returning/donating\n {user.get().title()}")
- with open ("books.txt", 'a') as f:
- f.write("\n"+user.get().title())
- btn_return=Button(win_return, text="Return", command=func_return, borderwidth=6, relief=RAISED)
- btn_return.pack()
- back=Button(win_return, text="Back", command=win_return.destroy, borderwidth=6, relief=RAISED)
- back.pack(side=BOTTOM, anchor='sw')
- win_return.mainloop()
- if __name__=='__main__':
- #In case when books.txt not found
- write_books='''
- Python Notes
- Theory of everything
- Out Look
- A Brief History of Time
- '''
- if (not os.path.exists("books.txt")):
- with open("books.txt", 'w') as f:
- f.write(write_books)
- #Opening books.txt file
- with open ("books.txt", 'r') as f:
- content=f.read()
- content=content.split("\n")
- #Creating GUI
- window=Library_gui(content)
- window.head_label()
- window.list_books_btn(window.list_books_func)
- window.borrow_btn(window.borrow_func)
- window.return_book_btn(window.return_func)
- window.about_btn()
- window.exit_btn()
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement