The following code is search_book.py, which will be imported into main.py
Full Code:
from tkinter import *
from tkinter import messagebox
from tkinter import StringVar
import tkinter.ttk
import pyautogui as pg
import mysql.connector as mc
def search_book():
global password, username
usern = pg.prompt("Username", "Enter your username")
passw = pg.password("Password", "Enter your password")
try:
conn = mc.connect(host="localhost", db="Library", user=usern, passwd=passw)
except:
messagebox.showerror("Incorrect Credentials", "Those credentials are incorrect")
exit()
global cur
cur = conn.cursor()
form = Tk()
form.geometry("500x200")
form.resizable(False, False)
form.title("Search for a book")
top_lbl = Label(form, text="Search For A Book", font="Courier 26").pack()
OptionList = [
"Pick A Search Type",
"Title",
"Author",
"ISBN",
]
global variable
variable = StringVar(form)
variable.set(OptionList[0])
search_dropd = OptionMenu(form, variable, *OptionList)
search_dropd.pack()
global search_entr
search_entr = Entry(form, width=45)
search_entr.pack()
submit_btn = Button(text="SUBMIT", command=submit, width=34).pack()
def submit():
search_words = search_entr.get()
search_type = variable.get()
if search_type == "Pick A Search Type":
messagebox.showerror("Invalid Choice", "'Pick A Search Type' is not a valid search type.")
else:
global results
if search_type == "ISBN":
cur.execute("SELECT * FROM Books WHERE ISBN = %s", (search_words,))
results = cur.fetchball()
if search_type == 'Title':
cur.execute("SELECT * FROM Books where ITLE = %s", (search_words,))
results = cur.fetchball()
if search_type == "Author":
cur.execute("SELECT * FROM Books WHERE AUTHOR = %s", (search_words,))
results = cur.fetchball()
show_results(results)
def show_results(results_list):
for i in range(len(results_list)):
print(results_list[i])