Added comments, fixed small issues, removed a few redundancies. Added run.bat.

This commit is contained in:
serialexperiments0815 2025-10-20 10:34:21 +02:00
parent 3d875f9b33
commit 8532e6318e
4 changed files with 40 additions and 14 deletions

View file

@ -1,20 +1,22 @@
import tkinter as tk import tkinter as tk
from tkinter import ttk
from buttonfade import buttonfade from buttonfade import buttonfade
#from Requests import search, image
from request_api import request_api from request_api import request_api
import requests import requests
from io import BytesIO from io import BytesIO
from PIL import Image, ImageTk from PIL import Image, ImageTk
# request_api class from request_api.py instantiated to use its api features inside this program.
rq = request_api() rq = request_api()
class BookSelection(): class BookSelection():
# Initialized with 0, the first book in the list (0).
def __init__(self): def __init__(self):
self.x = 0 self.x = 0
#self.imageisbn = None #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): def on_searchbutton(self):
self.x = 0 self.x = 0
searchbar_input = searchbar.get() searchbar_input = searchbar.get()
@ -26,13 +28,15 @@ class BookSelection():
def text_configuration(self): def text_configuration(self):
try: try:
resulttext.config(text=( resulttext.config(
text=(
f"{rq.Booktitle}" + "\n" f"{rq.Booktitle}" + "\n"
+ f"{rq.Bookauthor}" + "\n" + f"{rq.Bookauthor}" + "\n"
+ f"{rq.Bookisbn[self.x]}" + "\n" + f"{rq.Bookisbn[self.x]}" + "\n"
+ f"{rq.Bookdate[self.x]}" + "\n" + f"{rq.Bookdate[self.x]}" + "\n"
) )
) )
# If out of bounds start at position 0 and reinitialize.
except UnboundLocalError: except UnboundLocalError:
self.x = len(rq.return_isbn()) self.x = len(rq.return_isbn())
imageisbn = (rq.return_isbn(self.x)) imageisbn = (rq.return_isbn(self.x))
@ -40,6 +44,7 @@ class BookSelection():
self.text_configuration() self.text_configuration()
self.image_frame(imageisbn) self.image_frame(imageisbn)
# If IndexError (like out of bounds but went backwards) start at position 0 and reinitialize.
except IndexError: except IndexError:
self.x = 0 self.x = 0
imageisbn = (rq.return_isbn(self.x)) imageisbn = (rq.return_isbn(self.x))
@ -47,19 +52,20 @@ class BookSelection():
self.image_frame(imageisbn) self.image_frame(imageisbn)
def image_frame(self, imageisbn=None): 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) imager = rq.out_image(imageisbn)
response = requests.get(imager) response = requests.get(imager)
img_data = BytesIO(response.content) img_data = BytesIO(response.content)
photo = Image.open(img_data) photo = Image.open(img_data)
img = ImageTk.PhotoImage(photo) img = ImageTk.PhotoImage(photo)
imagelabel.config(image=img) imagelabel.config(image=img)
imagelabel.image = img imagelabel.image = img
# Button functionality made to be called by tkinter GUI buttons.
def on_leftright_button(self, direction): def on_leftright_button(self, direction):
internal_direction = direction internal_direction = direction
if internal_direction == "left": if internal_direction == "left":
@ -71,12 +77,15 @@ class BookSelection():
self.image_frame(imageisbn) self.image_frame(imageisbn)
self.text_configuration() self.text_configuration()
# If __name__ == "__main__" - Only runs when run directly (not through import).
if __name__ == "__main__": if __name__ == "__main__":
# Instantiates BookSelection Class for button usage.
BS = BookSelection() BS = BookSelection()
main = tk.Tk() main = tk.Tk()
main.geometry("600x600") main.geometry("600x800")
main.minsize(600, 800) main.minsize(600, 800)
main.maxsize(600, 800) main.maxsize(600, 800)
main.title("Book Finder") main.title("Book Finder")
@ -99,7 +108,7 @@ if __name__ == "__main__":
imagelabel = tk.Label(main) imagelabel = tk.Label(main)
imagelabel.grid(row=1, column=0, columnspan=2, sticky="nesw") 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 = tk.Button(main, text="Search", command=BS.on_searchbutton)
searchbutton.grid(row=2, column=2, sticky="nsew", pady=(40,0)) searchbutton.grid(row=2, column=2, sticky="nsew", pady=(40,0))
buttonleft = tk.Button(main, text="forward", command=lambda: BS.on_leftright_button("left")) 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 = tk.Canvas(main)
canvas.grid(row=1, column=2, sticky="nsew") 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 = tk.Scrollbar(main, orient=tk.VERTICAL, command=canvas.yview)
#scrollbar.grid(row=1, column=2, sticky="nsw") #scrollbar.grid(row=1, column=2, sticky="nsw")
# #
#canvas.configure(yscrollcommand=scrollbar.set) #canvas.configure(yscrollcommand=scrollbar.set)
#canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion = canvas.bbox("all"))) #canvas.bind('<Configure>', 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 = tk.Frame(canvas)
frame_two.grid(sticky="nsew") frame_two.grid(sticky="nsew")

View file

@ -1,5 +1,7 @@
import tkinter as tk 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): class buttonfade(tk.Entry):
def __init__(self, master=None, placeholder="Search Here", *args, **kwargs): def __init__(self, master=None, placeholder="Search Here", *args, **kwargs):
super().__init__(master, *args, **kwargs) super().__init__(master, *args, **kwargs)

View file

@ -1,5 +1,8 @@
import requests as rq 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(): class request_api():
@ -27,13 +30,16 @@ class request_api():
self.Bookisbn = Bookinfo.get('isbn') self.Bookisbn = Bookinfo.get('isbn')
self.Bookdate = Bookinfo.get('publish_date') 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): def return_title(self, x):
return self.Booktitle[x] return self.Booktitle[x]
def return_author(self, x): def return_author(self, x):
return self.Bookauthor[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): def return_isbn(self, x = None):
try: try:
if x != None: if x != None:
@ -46,12 +52,14 @@ class request_api():
def return_date(self, x): def return_date(self, x):
return self.Bookdate[x] return self.Bookdate[x]
# If "Imageisbn = None" (after search) - set imageisbn to bookisbn of 0
def out_image(self, imageisbn=None): def out_image(self, imageisbn=None):
if imageisbn is None: if imageisbn is None:
imageisbn = self.Bookisbn[0] imageisbn = self.Bookisbn[0]
image_output = f'https://covers.openlibrary.org/b/isbn/{imageisbn}-L.jpg' image_output = f'https://covers.openlibrary.org/b/isbn/{imageisbn}-L.jpg'
return image_output return image_output
# Small-Image functionality - not yet implemented.
def out_image_small(self, imageisbn=None): def out_image_small(self, imageisbn=None):
if imageisbn is None: if imageisbn is None:
imageisbn = self.Bookisbn[0] imageisbn = self.Bookisbn[0]

4
run.bat Normal file
View file

@ -0,0 +1,4 @@
@echo off
echo "starting"
python "Library.py"
pause