-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_Library.py
More file actions
381 lines (329 loc) · 14.5 KB
/
GUI_Library.py
File metadata and controls
381 lines (329 loc) · 14.5 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from Library import Library
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
WINDOW_SIZE = "500x500"
width = 600
height = 600
RESIZABLE_WINDOW = {
"y": 0,
"x": 0
}
class GuiImainPage:
def __init__(self):
self.root = Tk()
self.lib = Library()
self.MAIN_PAGE()
def MAIN_PAGE(self):
self.root.title('Library')
Label(self.root, text="Choose Login Or Register", height="2").pack()
Label(self.root, text="").pack()
Button(self.root, text="Login", height="2", width="30", command=self.login_window).pack()
Button(self.root, text="Register", height="2", width="30", command=self.register_windows).pack()
self.CONFIGURE_GUI(self.root, None, None)
self.root.mainloop()
def CONFIGURE_GUI(self, page, to_return, interface):
page.geometry(f"{width}x{height}")
page.resizable(RESIZABLE_WINDOW["y"], RESIZABLE_WINDOW["x"])
if page == self.root:
Button(page, text="CLOSE", height="1", width="15",
command=lambda: self.DESTROY_PAGE(destroy_page=page, return_page=to_return)) \
.pack(side=BOTTOM, pady=20)
elif interface:
pass
else:
Button(page, text="Return", height="1", width="15",
command=lambda: self.DESTROY_PAGE(destroy_page=page, return_page=to_return)) \
.pack(side=BOTTOM, pady=10)
# def SET_IMAGE(self):
# image = Image.open('bookImage.png')
# my_image = image.resize((height, width))
# self.bg = ImageTk.PhotoImage(my_image)
# self.bg_label = Label(self.root, image=self.bg)
# self.bg_label.place(x=0, y=0, relwidth=1,relheight=1)
def DESTROY_PAGE(self, destroy_page, return_page):
destroy_page.destroy()
return_page.wm_deiconify()
def register_windows(self):
register_page = Toplevel(self.root)
self.root.withdraw()
self.CONFIGURE_GUI(register_page, self.root, None)
register_page.title('Registry')
def register():
password_info = reg_password.get()
login_info = reg_login.get()
if not self.lib.password_available(password_info):
messagebox.askretrycancel("Error", "Unavailable password!")
if not self.lib.login_available(login_info):
if messagebox.askyesno("Error", "User with this Nickname exist, you want to login?"):
self.login_window()
register_page.destroy()
else:
register_page.destroy()
self.register_windows()
if self.lib.password_available(password_info) and self.lib.login_available(login_info):
self.lib.register_user(login_info, password_info)
# Tu raczej custom message box (Nie ma popup'u "PASS")
messagebox.showwarning("Successful", "User successful register!")
self.DESTROY_PAGE(register_page, self.root)
Label(register_page, text="Please input your 'Login' and 'Password' below").pack()
Label(register_page, text="").pack()
reg_login = StringVar()
Label(register_page, text="Username:").pack()
Entry(register_page, textvariable=reg_login).pack()
reg_password = StringVar()
Label(register_page, text="Password:").pack()
Entry(register_page, textvariable=reg_password, show="*").pack()
Button(register_page, text="Register", height="1", width="15",
command=register).pack(pady=20)
# Button(register_page, text="return", height="1", width="15",
# command=lambda:self.DESTROY_PAGE(destroy_page=register_page,return_page=self.root)).pack(pady=20)
def login_window(self):
login_page = Toplevel(self.root)
self.root.withdraw()
self.CONFIGURE_GUI(login_page, self.root, None)
login_page.title('Login window')
def log_in():
password_info = log_password.get()
login_info = log_login.get()
print(login_info)
print(password_info)
if self.lib.user_log_in(login_info, password_info):
# Tu raczej custom message box (Nie ma popup'u "PASS")
messagebox.showwarning("Successful", "User successful login!")
if self.lib.user_log_in(login_info, password_info) == "admin":
self.admin_window(login_info)
login_page.destroy()
else:
self.user_window(login_info)
login_page.destroy()
else:
if messagebox.askretrycancel("Error", "Username or password is incorrect, try again!"):
login_page.destroy()
self.login_window()
else:
login_page.destroy()
Label(login_page, text="Please input your 'Login' and 'Password' below").pack()
Label(login_page, text="").pack()
log_login = StringVar()
Label(login_page, text="Username:").pack()
Entry(login_page, textvariable=log_login).pack()
log_password = StringVar()
Label(login_page, text="Password:").pack()
Entry(login_page, textvariable=log_password, show="*").pack()
Button(login_page, text="Login", height="1", width="15",
command=log_in).pack(pady=20)
def user_window(self, login_info):
user_interface = Toplevel(self.root)
self.root.withdraw()
self.CONFIGURE_GUI(user_interface, self.root, True)
user_interface.title(f'Library {login_info}')
def borrow():
if self.lib.available_books_to_borrow():
borrow_window = Toplevel(user_interface)
user_interface.withdraw()
self.CONFIGURE_GUI(borrow_window, user_interface, None)
borrow_window.title(f'Library {login_info} - Borrow')
books = self.lib.available_books_to_borrow()
self.books_index = 0
self.actual_page_focus = 0
page_focus = {
0: 15,
1: 30,
2: 45,
3: 60
}
check_button_objects = []
self.books_list = []
def borrow_books():
selected_books = []
for i in range(1, 15):
if globals()[f"var{i}"].get():
selected_books.append(self.books_list[i - 1])
if selected_books:
self.lib.borrow(login_info, selected_books)
if messagebox.askyesno("Borrow next",
f"Book successful Borrowed. Are you want Borrow another?"):
borrow_window.destroy()
borrow()
else:
borrow_window.destroy()
user_interface.wm_deiconify()
Label(borrow_window, height=2, width=30,
text="Select books to borrow",
font=("Arial", 15)).pack(anchor=N)
def displayed_books(book_start_iteration, focused_page):
self.books_list = [book for book in books[book_start_iteration:page_focus[focused_page]]]
if not check_button_objects:
for i in range(0, 15):
globals()[f"var{i}"] = IntVar()
globals()[f"c{i}"] = ttk.Checkbutton(borrow_window,
variable=globals()[f"var{i}"],
text=self.books_list[i - 1],
onvalue=1)
globals()[f"c{i}"].pack(anchor=W, padx=15)
check_button_objects.append(globals()[f"c{i}"])
else:
for i in range(0, 15):
globals()[f"c{i}"].config(text=self.books_list[i - 1])
displayed_books(0, 0)
def change_page(next_prev_info):
if next_prev_info == "next":
if self.actual_page_focus + 1 in page_focus:
self.actual_page_focus += 1
self.books_index += 15
displayed_books(self.books_index, self.actual_page_focus)
else:
messagebox.showwarning("Error", "There is no more pages at the moment!")
elif next_prev_info == "prev":
if self.actual_page_focus - 1 in page_focus:
self.actual_page_focus -= 1
self.books_index -= 15
displayed_books(self.books_index, self.actual_page_focus)
else:
messagebox.showwarning("Error", "You're on first page")
label = Label(borrow_window, height=2, width=30)
label.pack()
button = Button(borrow_window, text="Borrow", command=borrow_books, height="2", width="30")
button.pack()
Button(borrow_window, text="Next Page", height="1", width="15",
command=lambda: change_page("next")).pack(side=RIGHT)
Button(borrow_window, text="Prev Page", height="1", width="15",
command=lambda: change_page("prev")).pack(side=LEFT)
else:
messagebox.showerror("Borrow Error", f"Oops! We don't have books at this moment. :(")
def deposit():
if self.lib.available_books_to_deposit(login_info):
deposit_window = Toplevel(user_interface)
user_interface.withdraw()
self.CONFIGURE_GUI(deposit_window, user_interface, None)
deposit_window.title(f'Library {login_info} - Deposit')
books = self.lib.available_books_to_deposit(login_info)
def on_select(event):
def lib_deposit():
self.lib.deposit(login_info, selected)
if messagebox.askyesno("Deposit next",
f"Book successful deposit. Are you want deposit another?"):
deposit_window.destroy()
deposit()
else:
deposit_window.destroy()
user_interface.wm_deiconify()
selected = event.widget.get()
label['text'] = f"You want deposit: {selected}?"
button['command'] = lib_deposit
combo = ttk.Combobox(deposit_window, values=books)
combo.pack(pady=10)
combo.bind('<<ComboboxSelected>>', on_select)
label = Label(deposit_window)
label.pack()
button = Button(deposit_window, text="Borrow", height="2", width="30")
button.pack()
else:
messagebox.showerror("Deposit", f"You don't have book to deposit. Borrow first some books :)")
# Button(deposit_window, text="return", height="1", width="15",
# command=lambda:self.DESTROY_PAGE(destroy_page=deposit_window,return_page=user_interface)).pack(pady=20)
def change_password():
change_password_window = Toplevel(user_interface)
user_interface.withdraw()
self.CONFIGURE_GUI(change_password_window, user_interface, None)
change_password_window.title(f'Library {login_info} - Change password')
def changing_password():
old_password_info = old_password.get()
new_password_info = new_password.get()
if self.lib.password_available(new_password_info):
self.lib.change_password(login_info, old_password_info, new_password_info)
if messagebox.showwarning("Successful", "Password successful changed! You need login again."):
change_password_window.destroy()
user_interface.destroy()
self.login_window()
old_password = StringVar()
Label(change_password_window, text="Old Password:").pack()
Entry(change_password_window, textvariable=old_password, show="*").pack()
new_password = StringVar()
Label(change_password_window, text="New_Password:").pack()
Entry(change_password_window, textvariable=new_password, show="*").pack()
Button(change_password_window, text="Change", height="2", width="30", command=changing_password).pack()
# Button(change_password_window, text="return", height="1", width="15",
# command=lambda:self.DESTROY_PAGE(destroy_page=change_password_window,return_page=user_interface)).pack(pady=20)
Label(user_interface, text="Whats you want do?", width="300", height="2").pack()
Label(user_interface, text="").pack()
Button(user_interface, text="Borrow", height="2", width="30", command=borrow).pack()
Button(user_interface, text="Deposit", height="2", width="30", command=deposit).pack()
Button(user_interface, text="Change Password", height="2", width="30", command=change_password).pack()
Button(user_interface, text="LOGOUT", height="1", width="15",
command=lambda: self.DESTROY_PAGE(destroy_page=user_interface, return_page=self.root)) \
.pack(pady=20)
def admin_window(self, login_info):
admin_interface = Toplevel(self.root)
self.root.withdraw()
self.CONFIGURE_GUI(admin_interface, self.root, True)
admin_interface.title(f'Library {login_info}')
def add_new_books():
new_books_window = Toplevel(admin_interface)
admin_interface.withdraw()
self.CONFIGURE_GUI(new_books_window, admin_interface, None)
new_books_window.title(f'Library {login_info} - Add new Books')
def add_book():
book_title_info = book_title.get()
book_author_info = book_author.get()
book_pages_info = book_pages.get()
if self.lib.can_be_add(book_title_info):
self.lib.add_new_book(book_title_info, book_author_info, book_pages_info)
if messagebox.askyesno("Successful",
f"Book '{book_title_info}' successful add. Are you want add next?"):
new_books_window.destroy()
add_new_books()
else:
new_books_window.destroy()
admin_interface.wm_deiconify()
else:
messagebox.showerror("Error", f"Book '{book_title_info}' is already in Library.")
book_title = StringVar()
Label(new_books_window, text="Book Title:").pack()
Entry(new_books_window, textvariable=book_title).pack()
book_author = StringVar()
Label(new_books_window, text="Book Author:").pack()
Entry(new_books_window, textvariable=book_author).pack()
book_pages = StringVar()
Label(new_books_window, text="Pages:").pack()
Entry(new_books_window, textvariable=book_pages).pack()
Button(new_books_window, text="Add Book", height="2", width="30", command=add_book).pack()
# Button(new_books_window, text="return", height="1", width="15",
# command=lambda: self.DESTROY_PAGE(destroy_page=new_books_window, return_page=admin_interface)).pack(pady=20)
def change_users_password():
psr_manager_window = Toplevel(admin_interface)
admin_interface.withdraw()
self.CONFIGURE_GUI(psr_manager_window, admin_interface, None)
psr_manager_window.title(f'Library {login_info} - Passwords manager')
def change_password():
user_name_info = user_name.get()
old_password_info = old_password.get()
new_password_info = new_password.get()
if self.lib.password_available(new_password_info):
self.lib.change_password(user_name_info, old_password_info, new_password_info)
if messagebox.showwarning("Successful",
f"Password for user:'{user_name_info}' successful changed!"):
psr_manager_window.destroy()
admin_interface.wm_deiconify()
user_name = StringVar()
Label(psr_manager_window, text="Username:").pack()
Entry(psr_manager_window, textvariable=user_name).pack()
old_password = StringVar()
Label(psr_manager_window, text="Old Password:").pack()
Entry(psr_manager_window, textvariable=old_password).pack()
new_password = StringVar()
Label(psr_manager_window, text="New Password:").pack()
Entry(psr_manager_window, textvariable=new_password).pack()
Button(psr_manager_window, text="Confirm", height="2", width="30", command=change_password).pack()
# Button(psr_manager_window, text="return", height="1", width="15",
# command=lambda: self.DESTROY_PAGE(destroy_page=psr_manager_window, return_page=admin_interface)).pack(pady=20)
Label(admin_interface, text="Whats you want do?", width="300", height="2").pack()
Label(admin_interface, text="").pack()
Button(admin_interface, text="Add Books", height="2", width="30",
command=add_new_books).pack()
Button(admin_interface, text="Password Manager", height="2", width="30",
command=change_users_password).pack()
# Button(admin_interface, text="LOGOUT", height="1", width="15",
# command=lambda: self.DESTROY_PAGE(destroy_page=admin_interface, return_page=self.root)).pack(pady=20)
GuiImainPage()