Repository navigation
Expand file tree
/
Copy pathbook.py
More file actions
452 lines (358 loc) · 17.5 KB
/
Copy pathbook.py
File metadata and controls
452 lines (358 loc) · 17.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import backend.backend as backend # 导入后端函数
class BookManagementWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.label = ttk.Label(self.frame, text="书籍管理")
self.label.pack(pady=10)
self.add_book_btn = ttk.Button(self.frame, text="添加书籍", command=self.app.show_add_book, width=30)
self.add_book_btn.pack(pady=5, ipady=5)
self.view_books_btn = ttk.Button(self.frame, text="查看书籍", command=self.app.show_view_books, width=30)
self.view_books_btn.pack(pady=5, ipady=5)
self.update_book_btn = ttk.Button(self.frame, text="更新书籍", command=self.app.show_update_book, width=30)
self.update_book_btn.pack(pady=5, ipady=5)
self.delete_book_btn = ttk.Button(self.frame, text="删除书籍", command=self.app.show_delete_book, width=30)
self.delete_book_btn.pack(pady=5, ipady=5)
self.search_books_btn = ttk.Button(self.frame, text="搜索书籍", command=self.app.show_search_book, width=30)
self.search_books_btn.pack(pady=5, ipady=5)
self.view_management_btn = ttk.Button(self.frame, text="更新视图和索引", command=self.app.show_update_view, width=30)
self.view_management_btn.pack(pady=5, ipady=5)
self.borrowing_info_btn = ttk.Button(self.frame, text="借书信息", command=self.app.show_borrowing_info, width=30)
self.borrowing_info_btn.pack(pady=5, ipady=5)
self.borrowing_search_btn = ttk.Button(self.frame, text="借书查询", command=self.app.show_borrowing_search, width=30)
self.borrowing_search_btn.pack(pady=5, ipady=5)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.main_menu), width=30)
self.back_btn.pack(pady=5, ipady=5)
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class AddBookWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
labels = ['书籍ID:', '标题:', '作者:', '出版社:', '译者:', '类别:', '到达时间 (YYYY-MM-DD):', '库存:']
self.entries = {}
for idx, text in enumerate(labels):
label = ttk.Label(self.frame, text=text)
label.grid(row=idx, column=0, padx=5, pady=5, sticky='e')
entry = ttk.Entry(self.frame)
entry.grid(row=idx, column=1, padx=5, pady=5)
self.entries[text] = entry
self.add_btn = ttk.Button(self.frame, text="添加书籍", command=self.add_book_to_db, width=15)
self.add_btn.grid(row=len(labels), column=0, columnspan=2, pady=10, ipady=5)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=15)
self.back_btn.grid(row=len(labels)+1, column=0, columnspan=2, pady=10, ipady=5)
def add_book_to_db(self):
book_data = (
self.entries['书籍ID:'].get(),
self.entries['标题:'].get(),
self.entries['作者:'].get(),
self.entries['出版社:'].get(),
self.entries['译者:'].get(),
self.entries['类别:'].get(),
self.entries['到达时间 (YYYY-MM-DD):'].get(),
self.entries['库存:'].get(),
)
success = backend.add_book(book_data)
if success:
messagebox.showinfo("成功", "书籍添加成功。")
self.app.go_back(self, self.app.book_management)
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class ViewBooksWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.selected_view = tk.StringVar()
self.create_widgets()
def create_widgets(self):
columns = ('书籍ID', '标题', '作者', '出版社', '译者', '类别', '到达时间', '库存')
self.tree = ttk.Treeview(self.frame, columns=columns, show='headings')
for col in columns:
self.tree.heading(col, text=col)
self.tree.pack(fill='both', expand=True)
# 添加下拉菜单选择视图
self.view_selector = ttk.Combobox(self.frame, textvariable=self.selected_view)
self.view_selector['values'] = ('view_all_books', 'view_books_by_category', 'view_books_by_author')
self.view_selector.current(0)
self.view_selector.bind('<<ComboboxSelected>>', self.load_books)
self.view_selector.pack(pady=10)
self.load_books()
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=15)
self.back_btn.pack(pady=10, ipady=5)
def load_books(self, event=None):
view = self.selected_view.get()
if view == 'view_all_books':
books = backend.get_all_books()
elif view == 'view_books_by_category':
books = backend.get_books_by_category()
elif view == 'view_books_by_author':
books = backend.get_books_by_author()
else:
books = []
# 清空当前树视图内容
for item in self.tree.get_children():
self.tree.delete(item)
# 插入新数据
for row in books:
self.tree.insert('', 'end', values=(
row['BookID'], row['Title'], row['Author'], row['Publisher'],
row['Translator'], row['Category'], row['ArrivalTime'], row['Stock']
))
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class UpdateBookWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.book_id_label = ttk.Label(self.frame, text="输入要更新的书籍ID:")
self.book_id_label.pack(pady=5)
self.book_id_entry = ttk.Entry(self.frame)
self.book_id_entry.pack(pady=5)
self.fetch_btn = ttk.Button(self.frame, text="获取数据", command=self.fetch_book_data)
self.fetch_btn.pack(pady=5, ipady=5)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=15)
self.back_btn.pack(pady=10, ipady=5)
def fetch_book_data(self):
book_id = self.book_id_entry.get()
book_data = backend.get_book_by_id(book_id)
if book_data:
self.show_update_form(book_data)
else:
messagebox.showerror("错误", "未找到书籍。")
def show_update_form(self, book_data):
self.update_window = tk.Toplevel(self.master)
self.update_window.title("更新书籍数据")
labels = ['标题:', '作者:', '出版社:', '译者:', '类别:', '到达时间 (YYYY-MM-DD):', '库存:']
self.entries = {}
for idx, text in enumerate(labels):
label = ttk.Label(self.update_window, text=text)
label.grid(row=idx, column=0, padx=5, pady=5, sticky='e')
entry = ttk.Entry(self.update_window)
entry.grid(row=idx, column=1, padx=5, pady=5)
self.entries[text] = entry
if text == '标题:':
entry.insert(0, book_data['Title'])
elif text == '作者:':
entry.insert(0, book_data['Author'])
elif text == '出版社:':
entry.insert(0, book_data['Publisher'])
elif text == '译者:':
entry.insert(0, book_data['Translator'])
elif text == '类别:':
entry.insert(0, book_data['Category'])
elif text == '到达时间 (YYYY-MM-DD):':
entry.insert(0, book_data['ArrivalTime'])
elif text == '库存:':
entry.insert(0, book_data['Stock'])
self.update_btn = ttk.Button(self.update_window, text="更新书籍",
command=lambda: self.update_book_in_db(book_data['BookID']), width=15)
self.update_btn.grid(row=len(labels), column=0, columnspan=2, pady=10, ipady=5)
def update_book_in_db(self, book_id):
book_data = (
self.entries['标题:'].get(),
self.entries['作者:'].get(),
self.entries['出版社:'].get(),
self.entries['译者:'].get(),
self.entries['类别:'].get(),
self.entries['到达时间 (YYYY-MM-DD):'].get(),
self.entries['库存:'].get(),
)
success = backend.update_book(book_id, book_data)
if success:
messagebox.showinfo("成功", "书籍更新成功。")
self.update_window.destroy()
self.app.go_back(self, self.app.book_management)
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class DeleteBookWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.book_id_label = ttk.Label(self.frame, text="输入要删除的书籍ID:")
self.book_id_label.pack(pady=5)
self.book_id_entry = ttk.Entry(self.frame)
self.book_id_entry.pack(pady=5)
self.delete_btn = ttk.Button(self.frame, text="删除书籍", command=self.delete_book_from_db)
self.delete_btn.pack(pady=5, ipady=5)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=15)
self.back_btn.pack(pady=10, ipady=5)
def delete_book_from_db(self):
book_id = self.book_id_entry.get()
success = backend.delete_book(book_id)
if success:
messagebox.showinfo("成功", "书籍删除成功。")
self.app.go_back(self, self.app.book_management)
else:
messagebox.showerror("错误", "未找到书籍或无法删除。")
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class SearchBooksWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.label = ttk.Label(self.frame, text="搜索书籍")
self.label.pack(pady=10)
self.id_label = ttk.Label(self.frame, text="书籍ID:")
self.id_label.pack(pady=5)
self.id_entry = ttk.Entry(self.frame)
self.id_entry.pack(pady=5)
self.title_label = ttk.Label(self.frame, text="书名:")
self.title_label.pack(pady=5)
self.title_entry = ttk.Entry(self.frame)
self.title_entry.pack(pady=5)
self.author_label = ttk.Label(self.frame, text="作者:")
self.author_label.pack(pady=5)
self.author_entry = ttk.Entry(self.frame)
self.author_entry.pack(pady=5)
self.category_label = ttk.Label(self.frame, text="类别:")
self.category_label.pack(pady=5)
self.category_entry = ttk.Entry(self.frame)
self.category_entry.pack(pady=5)
self.search_btn = ttk.Button(self.frame, text="搜索", command=self.search_books)
self.search_btn.pack(pady=10, ipady=5)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=30)
self.back_btn.pack(pady=5, ipady=5)
self.tree = ttk.Treeview(self.frame, columns=('书籍ID', '标题', '作者', '出版社', '译者', '类别', '到达时间', '库存'), show='headings')
for col in self.tree['columns']:
self.tree.heading(col, text=col)
self.tree.pack(fill='both', expand=True)
def search_books(self):
book_id = self.id_entry.get()
title = self.title_entry.get()
author = self.author_entry.get()
category = self.category_entry.get()
# 将空字符串转换为 None
book_id = book_id if book_id else None
title = title if title else None
author = author if author else None
category = category if category else None
books = backend.search_books(book_id=book_id, title=title, author=author, category=category)
for row in self.tree.get_children():
self.tree.delete(row)
for book in books:
self.tree.insert('', 'end', values=(book['BookID'], book['Title'], book['Author'], book['Publisher'], book['Translator'], book['Category'], book['ArrivalTime'], book['Stock']))
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class UpdateViewManageWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.label = ttk.Label(self.frame, text="更新视图和索引")
self.label.pack(pady=10)
self.update_views_btn = ttk.Button(self.frame, text="更新视图", command=self.update_views, width=30)
self.update_views_btn.pack(pady=5, ipady=5)
self.update_index_btn = ttk.Button(self.frame, text="更新索引", command=self.update_index, width=30)
self.update_index_btn.pack(pady=5, ipady=5)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=30)
self.back_btn.pack(pady=5, ipady=5)
def update_views(self):
success = backend.update_views()
if success:
messagebox.showinfo("成功", "视图更新成功。")
else:
messagebox.showerror("错误", "视图更新失败。")
def update_index(self):
success = backend.update_index()
if success:
messagebox.showinfo("成功", "索引更新成功。")
else:
messagebox.showerror("错误", "索引更新失败。")
def show(self):
print("Showing UpdateViewManageWindow")
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class BorrowingInfoWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.label = ttk.Label(self.frame, text="借书信息")
self.label.pack(pady=10)
columns = ('学生姓名', '学生ID', '书籍ID', '书籍名称', '借书时间', '还书时间')
self.tree = ttk.Treeview(self.frame, columns=columns, show='headings')
for col in columns:
self.tree.heading(col, text=col)
self.tree.pack(fill='both', expand=True)
self.load_borrowing_info()
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=30)
self.back_btn.pack(pady=10, ipady=5)
def load_borrowing_info(self):
borrowing_info = backend.get_borrowing_info()
for row in self.tree.get_children():
self.tree.delete(row)
for info in borrowing_info:
self.tree.insert('', 'end', values=(info['StudentName'], info['StudentID'], info['BookID'], info['BookTitle'], info['BorrowDate'], info['ReturnDate']))
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()
class BorrowingSearchWindow:
def __init__(self, master, app):
self.master = master
self.app = app
self.frame = ttk.Frame(self.master)
self.create_widgets()
def create_widgets(self):
self.label = ttk.Label(self.frame, text="借书信息")
self.label.pack(pady=10)
self.student_id_label = ttk.Label(self.frame, text="学生ID:")
self.student_id_label.pack(pady=5)
self.student_id_entry = ttk.Entry(self.frame)
self.student_id_entry.pack(pady=5)
self.search_btn = ttk.Button(self.frame, text="查询", command=self.search_borrowing_info)
self.search_btn.pack(pady=10, ipady=5)
columns = ('学生姓名', '学生ID', '书籍ID', '书籍名称', '借书时间', '还书时间')
self.tree = ttk.Treeview(self.frame, columns=columns, show='headings')
for col in columns:
self.tree.heading(col, text=col)
self.tree.pack(fill='both', expand=True)
self.back_btn = ttk.Button(self.frame, text="返回", command=lambda: self.app.go_back(self, self.app.book_management), width=30)
self.back_btn.pack(pady=10, ipady=5)
def search_borrowing_info(self):
student_id = self.student_id_entry.get()
borrowing_info = backend.get_borrowing_info_by_student(student_id)
for row in self.tree.get_children():
self.tree.delete(row)
for info in borrowing_info:
self.tree.insert('', 'end', values=(info['StudentName'], info['StudentID'], info['BookID'], info['BookTitle'], info['BorrowDate'], info['ReturnDate']))
def show(self):
self.frame.pack(fill='both', expand=True)
def hide(self):
self.frame.pack_forget()