import tkinter as tk from buttonfade import buttonfade 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() rq.search(searchbar_input) self.image_frame() self.text_configuration() for number in range(len(rq.return_isbn())): tk.Label(frame_two, text=f"{rq.return_isbn(number)}").grid(row=number, column=0, pady=10, padx=20, sticky="ew") def text_configuration(self): try: 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)) print(imageisbn) 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)) self.text_configuration() self.image_frame(imageisbn) def image_frame(self, imageisbn=None): # 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": self.x += 1 imageisbn = (rq.return_isbn(self.x)) else: self.x -= 1 imageisbn = (rq.return_isbn(self.x)) 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("600x800") main.minsize(600, 800) main.maxsize(600, 800) main.title("Book Finder") main.rowconfigure(0, weight=1, minsize=50) main.rowconfigure(1, weight=1, minsize=660) main.rowconfigure(2, weight=1, minsize=90) main.columnconfigure(0, weight=1, minsize=200) main.columnconfigure(1, weight=1, minsize=200) main.columnconfigure(2, weight=1, minsize=200) searchbar = buttonfade(main, placeholder="Search here", justify="center") searchbar.grid(row=0, column=0, columnspan=3, sticky="nesw") resulttext = tk.Label(main, text="", wraplength=600) resulttext.grid(row=2, column=0, columnspan=2, sticky="nesw") 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")) buttonleft.grid(row=2, column=2, sticky="nsew", padx=(100, 0), pady=(0,50)) buttonright = tk.Button(main, text="back", command=lambda: BS.on_leftright_button("right")) buttonright.grid(row=2, column=2, sticky="nswe", padx=(0, 100), pady=(0,50)) 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") canvas.create_window((0, 0), window=frame_two, anchor="nw") main.mainloop()