From 8532e6318e0435798dd998742ea9fced46716646 Mon Sep 17 00:00:00 2001 From: serialexperiments0815 <112315380+serialexperiments0815@users.noreply.github.com> Date: Mon, 20 Oct 2025 10:34:21 +0200 Subject: [PATCH] Added comments, fixed small issues, removed a few redundancies. Added run.bat. --- Library.py | 38 +++++++++++++++++++++++++------------- buttonfade.py | 2 ++ request_api.py | 10 +++++++++- run.bat | 4 ++++ 4 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 run.bat diff --git a/Library.py b/Library.py index f810534..cb6c4dc 100644 --- a/Library.py +++ b/Library.py @@ -1,20 +1,22 @@ import tkinter as tk -from tkinter import ttk from buttonfade import buttonfade -#from Requests import search, image from request_api import request_api import requests from io import BytesIO from PIL import Image, ImageTk +# request_api class from request_api.py instantiated to use its api features inside this program. rq = request_api() class BookSelection(): - + # Initialized with 0, the first book in the list (0). def __init__(self): self.x = 0 #self.imageisbn = None + # When search button pressed the method starts from 0 to allow for multiple searches during program use without conflict. + # Initializes self.image_frame() and self.text_configuration() methods to process data for GUI. + # Then runs through forloop in range of return_isbn() list range to fill right side of GUI with ISBN numbers. def on_searchbutton(self): self.x = 0 searchbar_input = searchbar.get() @@ -26,13 +28,15 @@ class BookSelection(): def text_configuration(self): try: - resulttext.config(text=( + resulttext.config( + text=( f"{rq.Booktitle}" + "\n" + f"{rq.Bookauthor}" + "\n" + f"{rq.Bookisbn[self.x]}" + "\n" + f"{rq.Bookdate[self.x]}" + "\n" ) ) + # If out of bounds start at position 0 and reinitialize. except UnboundLocalError: self.x = len(rq.return_isbn()) imageisbn = (rq.return_isbn(self.x)) @@ -40,6 +44,7 @@ class BookSelection(): self.text_configuration() self.image_frame(imageisbn) + # If IndexError (like out of bounds but went backwards) start at position 0 and reinitialize. except IndexError: self.x = 0 imageisbn = (rq.return_isbn(self.x)) @@ -47,19 +52,20 @@ class BookSelection(): self.image_frame(imageisbn) def image_frame(self, imageisbn=None): - global photo + # Assign image url to variable. Then get image url through requests module. + # Decode output through BytesIO module. Open decoded image data through Image module. + # Create an object of opened decoded image through ImageTK module. Configure Imagelabel + # to use it through .config method parameter assignment and then assign imagelabel.image method as = img. + # This method is solely responsible for 3 module implementations. imager = rq.out_image(imageisbn) response = requests.get(imager) img_data = BytesIO(response.content) - photo = Image.open(img_data) - img = ImageTk.PhotoImage(photo) - imagelabel.config(image=img) - imagelabel.image = img + # Button functionality made to be called by tkinter GUI buttons. def on_leftright_button(self, direction): internal_direction = direction if internal_direction == "left": @@ -71,12 +77,15 @@ class BookSelection(): self.image_frame(imageisbn) self.text_configuration() +# If __name__ == "__main__" - Only runs when run directly (not through import). if __name__ == "__main__": + + # Instantiates BookSelection Class for button usage. BS = BookSelection() main = tk.Tk() - main.geometry("600x600") + main.geometry("600x800") main.minsize(600, 800) main.maxsize(600, 800) main.title("Book Finder") @@ -99,7 +108,7 @@ if __name__ == "__main__": imagelabel = tk.Label(main) imagelabel.grid(row=1, column=0, columnspan=2, sticky="nesw") - + # Button usage is here. 3 Buttons. searchbutton = tk.Button(main, text="Search", command=BS.on_searchbutton) searchbutton.grid(row=2, column=2, sticky="nsew", pady=(40,0)) buttonleft = tk.Button(main, text="forward", command=lambda: BS.on_leftright_button("left")) @@ -112,12 +121,15 @@ if __name__ == "__main__": canvas = tk.Canvas(main) canvas.grid(row=1, column=2, sticky="nsew") + # Failed attempts at adding scrollbar functionality to ISBN-Book list on the right. #scrollbar = tk.Scrollbar(main, orient=tk.VERTICAL, command=canvas.yview) #scrollbar.grid(row=1, column=2, sticky="nsw") -# + # #canvas.configure(yscrollcommand=scrollbar.set) #canvas.bind('', lambda e: canvas.configure(scrollregion = canvas.bbox("all"))) -# + # + + # Seperate canvas for Book image stretched to all four direction by sticky. frame_two = tk.Frame(canvas) frame_two.grid(sticky="nsew") diff --git a/buttonfade.py b/buttonfade.py index ed8780f..c8b1327 100644 --- a/buttonfade.py +++ b/buttonfade.py @@ -1,5 +1,7 @@ import tkinter as tk +# This buttonfade class was implemented soly for it's function of giving placeholder-text in the searchbar +# which disappears when clicked into. (User-Friendly design). class buttonfade(tk.Entry): def __init__(self, master=None, placeholder="Search Here", *args, **kwargs): super().__init__(master, *args, **kwargs) diff --git a/request_api.py b/request_api.py index 7f3fcd2..d975fca 100644 --- a/request_api.py +++ b/request_api.py @@ -1,5 +1,8 @@ import requests as rq +# Request_api class made for import in the Library.py file. Seperate for better overview. +# Class intializes five parameters first to then have them assigned by requests get method. +# Requests are made to openlibrary, because they support api calls. class request_api(): @@ -27,13 +30,16 @@ class request_api(): self.Bookisbn = Bookinfo.get('isbn') self.Bookdate = Bookinfo.get('publish_date') - + # Return methods implemented for standardized retrieval of selected book properties. + # Also for easy implementation of the next and previous book buttons while searching. def return_title(self, x): return self.Booktitle[x] def return_author(self, x): return self.Bookauthor[x] + # If x (place in gotten book que) is None (after search) return entire list. + # If IndexError (clicked next past que list) then return to first (zero) book entry in list. def return_isbn(self, x = None): try: if x != None: @@ -46,12 +52,14 @@ class request_api(): def return_date(self, x): return self.Bookdate[x] + # If "Imageisbn = None" (after search) - set imageisbn to bookisbn of 0 def out_image(self, imageisbn=None): if imageisbn is None: imageisbn = self.Bookisbn[0] image_output = f'https://covers.openlibrary.org/b/isbn/{imageisbn}-L.jpg' return image_output + # Small-Image functionality - not yet implemented. def out_image_small(self, imageisbn=None): if imageisbn is None: imageisbn = self.Bookisbn[0] diff --git a/run.bat b/run.bat new file mode 100644 index 0000000..2fd9dcc --- /dev/null +++ b/run.bat @@ -0,0 +1,4 @@ +@echo off +echo "starting" +python "Library.py" +pause \ No newline at end of file