1+ import tkinter as tk
2+ from tkinter import messagebox , ttk
3+ import csv
4+ import os
5+
6+ # Load books from CSV file
7+ def load_books ():
8+ books = []
9+ try :
10+ # Debugging: Print the current working directory
11+ print ("Current working directory:" , os .getcwd ())
12+
13+ # Attempt to open the CSV file
14+ with open ("LibraryBooks.csv" , mode = "r" , encoding = "utf-8" ) as file :
15+ reader = csv .DictReader (file )
16+
17+ # Debugging: Print the headers of the CSV file
18+ print ("CSV Headers:" , reader .fieldnames )
19+
20+ for row in reader :
21+ books .append ({
22+ "Book_Title" : row .get ("Book_Title" , "" ).strip (),
23+ "Author" : row .get ("Author" , "" ).strip (),
24+ "Barcode" : row .get ("Barcode" , "" ).strip (),
25+ "Status" : row .get ("Status" , "" ).strip (),
26+ "User" : row .get ("User" , "" ).strip ()
27+ })
28+
29+ # Debugging: Print the loaded books
30+ print ("Books loaded:" , books )
31+
32+ except FileNotFoundError :
33+ messagebox .showerror ("Error" , "LibraryBooks.csv file not found." )
34+ except Exception as e :
35+ messagebox .showerror ("Error" , f"An error occurred while loading books: { e } " )
36+ return books
37+
38+ # Save books to CSV file
39+ def save_books (books ):
40+ try :
41+ with open ("LibraryBooks.csv" , mode = "w" , encoding = "utf-8" , newline = "" ) as file :
42+ fieldnames = ["Book_Title" , "Author" , "Barcode" , "Status" , "User" ]
43+ writer = csv .DictWriter (file , fieldnames = fieldnames )
44+ writer .writeheader ()
45+ writer .writerows (books )
46+ except Exception as e :
47+ messagebox .showerror ("Error" , f"An error occurred while saving books: { e } " )
48+
49+ # Search books
50+ def search_books ():
51+ query = search_entry .get ().strip ().lower ()
52+ search_by = search_by_var .get ()
53+ results = []
54+ for book in books :
55+ if query in book [search_by ].lower ():
56+ results .append (book )
57+ update_treeview (results )
58+
59+ # Check out a book
60+ def check_out_book ():
61+ barcode = barcode_entry .get ().strip ()
62+ user = user_entry .get ().strip ()
63+ if not barcode or not user :
64+ messagebox .showerror ("Error" , "Please enter both barcode and user name." )
65+ return
66+ for book in books :
67+ if book ["Barcode" ] == barcode :
68+ if book ["Status" ] == "Available" :
69+ book ["Status" ] = "Checked Out"
70+ book ["User" ] = user
71+ save_books (books )
72+ update_treeview (books )
73+ messagebox .showinfo ("Success" , f'Book "{ book ["Book_Title" ]} " checked out by { user } .' )
74+ return
75+ else :
76+ messagebox .showerror ("Error" , f'Book "{ book ["Book_Title" ]} " is already checked out.' )
77+ return
78+ messagebox .showerror ("Error" , "Barcode not found." )
79+
80+ # Check in a book
81+ def check_in_book ():
82+ barcode = barcode_entry .get ().strip ()
83+ if not barcode :
84+ messagebox .showerror ("Error" , "Please enter the barcode." )
85+ return
86+ for book in books :
87+ if book ["Barcode" ] == barcode :
88+ if book ["Status" ] == "Checked Out" :
89+ book ["Status" ] = "Available"
90+ book ["User" ] = ""
91+ save_books (books )
92+ update_treeview (books )
93+ messagebox .showinfo ("Success" , f'Book "{ book ["Book_Title" ]} " checked in.' )
94+ return
95+ else :
96+ messagebox .showerror ("Error" , f'Book "{ book ["Book_Title" ]} " is already available.' )
97+ return
98+ messagebox .showerror ("Error" , "Barcode not found." )
99+
100+ # Update treeview with books
101+ def update_treeview (data ):
102+ for item in tree .get_children ():
103+ tree .delete (item )
104+ for book in data :
105+ tree .insert ("" , "end" , values = (book ["Book_Title" ], book ["Author" ], book ["Barcode" ], book ["Status" ], book ["User" ]))
106+
107+ # Load books
108+ books = load_books ()
109+
110+ # Debugging: Check if books list is empty
111+ if not books :
112+ print ("No books loaded. Please check the CSV file." )
113+
114+ # Create GUI
115+ root = tk .Tk ()
116+ root .title ("Library Management System" )
117+
118+ # Search Section
119+ search_frame = tk .Frame (root )
120+ search_frame .pack (pady = 10 )
121+ search_label = tk .Label (search_frame , text = "Search:" )
122+ search_label .pack (side = "left" , padx = 5 )
123+ search_entry = tk .Entry (search_frame )
124+ search_entry .pack (side = "left" , padx = 5 )
125+ search_by_var = tk .StringVar (value = "Book_Title" )
126+ search_by_menu = ttk .Combobox (search_frame , textvariable = search_by_var , values = ["Book_Title" , "Author" , "User" ], state = "readonly" )
127+ search_by_menu .pack (side = "left" , padx = 5 )
128+ search_button = tk .Button (search_frame , text = "Search" , command = search_books )
129+ search_button .pack (side = "left" , padx = 5 )
130+
131+ # Treeview for displaying books
132+ tree = ttk .Treeview (root , columns = ("Book_Title" , "Author" , "Barcode" , "Status" , "User" ), show = "headings" )
133+ tree .heading ("Book_Title" , text = "Book Title" )
134+ tree .heading ("Author" , text = "Author" )
135+ tree .heading ("Barcode" , text = "Barcode" )
136+ tree .heading ("Status" , text = "Status" )
137+ tree .heading ("User" , text = "User" )
138+ tree .pack (pady = 10 , fill = "both" , expand = True )
139+ update_treeview (books )
140+
141+ # Check Out/In Section
142+ action_frame = tk .Frame (root )
143+ action_frame .pack (pady = 10 )
144+ barcode_label = tk .Label (action_frame , text = "Barcode:" )
145+ barcode_label .pack (side = "left" , padx = 5 )
146+ barcode_entry = tk .Entry (action_frame )
147+ barcode_entry .pack (side = "left" , padx = 5 )
148+ user_label = tk .Label (action_frame , text = "User:" )
149+ user_label .pack (side = "left" , padx = 5 )
150+ user_entry = tk .Entry (action_frame )
151+ user_entry .pack (side = "left" , padx = 5 )
152+ check_out_button = tk .Button (action_frame , text = "Check Out" , command = check_out_book )
153+ check_out_button .pack (side = "left" , padx = 5 )
154+ check_in_button = tk .Button (action_frame , text = "Check In" , command = check_in_book )
155+ check_in_button .pack (side = "left" , padx = 5 )
156+
157+ # Run the application
158+ root .mainloop ()
0 commit comments