-
Notifications
You must be signed in to change notification settings - Fork 0
/
mybooks.py
176 lines (131 loc) · 5.91 KB
/
mybooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from tkinter import Tk, Button,Label,Scrollbar,Listbox,StringVar,Entry,W,E,N,S,END
from tkinter import ttk
from tkinter import messagebox
from mysql_config import dbConfig
import mysql.connector as pyo
con = pyo.connect(**dbConfig)
#print(con)
cursor = con.cursor()
class Bookdb:
def __init__(self):
self.con = pyo.connect(**dbConfig)
self.cursor = con.cursor()
print("You have connected to the database")
print(con)
def __del__(self):
self.con.close()
def view(self):
self.cursor.execute("SELECT * FROM books")
rows = self.cursor.fetchall()
return rows
def insert(self,title, author, isbn):
sql=("INSERT INTO books(title,author,isbn)VALUES (%s,%s,%s)")
values =[title,author,isbn]
self.cursor.execute(sql,values)
self.con.commit()
messagebox.showinfo(title="Book Database",message="New book added to database")
def update(self, id, title, author, isbn):
tsql = 'UPDATE books SET title = %s, author = %s, isbn = %s WHERE id=%s'
self.cursor.execute(tsql, [title,author,isbn,id])
self.con.commit()
messagebox.showinfo(title="Book Database",message="Book Updated")
def delete(self, id):
delquery ='DELETE FROM books WHERE id = %s'
self.cursor.execute(delquery, [id])
self.con.commit()
messagebox.showinfo(title="Book Database",message="Book Deleted")
db = Bookdb()
def get_selected_row(event):
global selected_tuple
index = list_bx.curselection()#[0]
selected_tuple = list_bx.get(index)
if len(selected_tuple) > 3:
title_entry.delete(0, 'end')
title_entry.insert('end', selected_tuple[1])
author_entry.delete(0, 'end')
author_entry.insert('end', selected_tuple[2])
isbn_entry.delete(0, 'end')
isbn_entry.insert('end', selected_tuple[3])
else:
title_entry.delete(0, 'end')
title_entry.insert('end', selected_tuple[0])
author_entry.delete(0, 'end')
author_entry.insert('end', selected_tuple[1])
isbn_entry.delete(0, 'end')
isbn_entry.insert('end', selected_tuple[2])
def view_records():
list_bx.delete(0, 'end')
for row in db.view():
list_bx.insert('end', row)
def add_book():
db.insert(title_text.get(),author_text.get(),isbn_text.get())
list_bx.delete(0, 'end')
list_bx.insert('end', (title_text.get(), author_text.get(), isbn_text.get()))
title_entry.delete(0, "end") # Clears input after inserting
author_entry.delete(0, "end")
isbn_entry.delete(0, "end")
con.commit()
def delete_records():
db.delete(selected_tuple[0])
con.commit()
def clear_screen():
list_bx.delete(0,'end')
title_entry.delete(0,'end')
author_entry.delete(0,'end')
isbn_entry.delete(0,'end')
def update_records():
db.update(selected_tuple[0], title_text.get(), author_text.get(), isbn_text.get()) #removed selected_tuple[0]
title_entry.delete(0, "end") # Clears input after inserting
author_entry.delete(0, "end")
isbn_entry.delete(0, "end")
con.commit()
def on_closing():
dd = db
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
del dd
root = Tk() # Creates application window
root.title("My Books Database Application") # Adds a title to application window
root.configure(background="light green") # Add background color to application window
root.geometry("1000x500") # Sets a size for application window
root.resizable(width=False,height=False) # Prevents the application window from resizing
# Create Labels and entry widgets
title_label =ttk.Label(root,text="Title",background="light green",font=("TkDefaultFont", 16))
title_label.grid(row=0, column=0, sticky=W)
title_text = StringVar()
title_entry = ttk.Entry(root,width=24,textvariable=title_text)
title_entry.grid(row=0, column=1, sticky=W)
author_label =ttk.Label(root,text="Author",background="light green",font=("TkDefaultFont", 16))
author_label.grid(row=0, column=2, sticky=W)
author_text = StringVar()
author_entry = ttk.Entry(root,width=24,textvariable=author_text)
author_entry.grid(row=0, column=3, sticky=W)
isbn_label =ttk.Label(root,text="ISBN",background="light green",font=("TkDefaultFont", 14))
isbn_label.grid(row=0, column=4, sticky=W)
isbn_text = StringVar()
isbn_entry = ttk.Entry(root,width=24,textvariable=isbn_text)
isbn_entry.grid(row=0, column=5, sticky=W)
# Add a button to insert inputs into database
add_btn = Button(root, text="Add Book",bg="blue",fg="white",font="helvetica 10 bold",command=add_book)
add_btn.grid(row=0, column=6, sticky=W)
# Add a listbox to display data from database
list_bx = Listbox(root,height=16,width=40,font="helvetica 13",bg="light blue")
list_bx.grid(row=3,column=1, columnspan=14,sticky=W + E,pady=40,padx=15)
list_bx.bind('<<ListboxSelect>>',get_selected_row)
# Add scrollbar to enable scrolling
scroll_bar = Scrollbar(root)
scroll_bar.grid(row=1,column=8, rowspan=14,sticky=W )
list_bx.configure(yscrollcommand=scroll_bar.set) # Enables vetical scrolling
scroll_bar.configure(command=list_bx.yview)
# Add more Button Widgets
modify_btn = Button(root, text="Modify Record",bg="purple",fg="white",font="helvetica 10 bold",command=update_records)
modify_btn.grid(row=15, column=4)
delete_btn = Button(root, text="Delete Record",bg="red",fg="white",font="helvetica 10 bold",command=delete_records)
delete_btn.grid(row=15, column=5)
view_btn = Button(root, text="View all records",bg="black",fg="white",font="helvetica 10 bold",command=view_records)
view_btn.grid(row=15, column=1)#, sticky=tk.N)
clear_btn = Button(root, text="Clear Screen",bg="maroon",fg="white",font="helvetica 10 bold",command=clear_screen)
clear_btn.grid(row=15, column=2)#, sticky=tk.W)
exit_btn = Button(root, text="Exit Application",bg="blue",fg="white",font="helvetica 10 bold",command=root.destroy)
exit_btn.grid(row=15, column=3)
root.mainloop() # Runs the application until exit