|
|
|
@ -63,6 +63,26 @@ class GUI:
|
|
|
|
|
bg='white',
|
|
|
|
|
padx=10,
|
|
|
|
|
pady=10)
|
|
|
|
|
|
|
|
|
|
# 添加模糊搜索按钮
|
|
|
|
|
self.fuzzy_button = tk.Button(self.main_frame,
|
|
|
|
|
text="模糊搜索",
|
|
|
|
|
command=self.fuzzy_search,
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
bg='#2196F3', # 蓝色按钮
|
|
|
|
|
fg='white',
|
|
|
|
|
activebackground='#0b7dda',
|
|
|
|
|
padx=15,
|
|
|
|
|
pady=5)
|
|
|
|
|
|
|
|
|
|
# 添加模糊搜索输入框
|
|
|
|
|
self.fuzzy_entry00 = tk.StringVar()
|
|
|
|
|
self.fuzzy_entry = tk.Entry(self.input_frame,
|
|
|
|
|
textvariable=self.fuzzy_entry00,
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
width=30,
|
|
|
|
|
relief=tk.GROOVE,
|
|
|
|
|
borderwidth=2)
|
|
|
|
|
|
|
|
|
|
def setup_layout(self):
|
|
|
|
|
"""设置组件布局"""
|
|
|
|
@ -78,6 +98,39 @@ class GUI:
|
|
|
|
|
self.query_button.pack(pady=15)
|
|
|
|
|
self.result_frame.pack(fill='both', expand=True, pady=(10,0))
|
|
|
|
|
self.result_label.pack(expand=True, fill='both')
|
|
|
|
|
|
|
|
|
|
# 添加模糊搜索输入框和按钮布局
|
|
|
|
|
tk.Label(self.input_frame,
|
|
|
|
|
text="模糊搜索:",
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
bg='#f0f0f0').grid(row=1, column=0, padx=5)
|
|
|
|
|
self.fuzzy_entry.grid(row=1, column=1, padx=5)
|
|
|
|
|
self.fuzzy_button.pack(pady=10)
|
|
|
|
|
|
|
|
|
|
def fuzzy_search(self):
|
|
|
|
|
"""处理模糊搜索按钮点击事件"""
|
|
|
|
|
keyword = self.fuzzy_entry00.get().strip()
|
|
|
|
|
if not keyword:
|
|
|
|
|
self.result_label.config(text="请输入搜索关键词")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.fuzzy_button.config(state=tk.DISABLED, text="搜索中...")
|
|
|
|
|
self.root.update()
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
results = fuzzy_search_book(keyword)
|
|
|
|
|
if isinstance(results, str):
|
|
|
|
|
self.result_label.config(text=results)
|
|
|
|
|
else:
|
|
|
|
|
formatted_results = "\n\n".join(
|
|
|
|
|
"\n".join(f"{k}: {v}" for k, v in book.items())
|
|
|
|
|
for book in results
|
|
|
|
|
)
|
|
|
|
|
self.result_label.config(text=formatted_results)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.result_label.config(text=f"搜索出错: {str(e)}")
|
|
|
|
|
finally:
|
|
|
|
|
self.fuzzy_button.config(state=tk.NORMAL, text="模糊搜索")
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
"""处理查询按钮点击事件"""
|
|
|
|
@ -104,6 +157,56 @@ class GUI:
|
|
|
|
|
# 恢复按钮状态
|
|
|
|
|
self.query_button.config(state=tk.NORMAL, text="查询")
|
|
|
|
|
|
|
|
|
|
def display_results(self, results):
|
|
|
|
|
"""以表格形式显示查询结果"""
|
|
|
|
|
# 清空原有内容
|
|
|
|
|
for widget in self.result_frame.winfo_children():
|
|
|
|
|
widget.destroy()
|
|
|
|
|
|
|
|
|
|
if isinstance(results, str) or (isinstance(results, list) and len(results) == 1 and isinstance(results[0], str)):
|
|
|
|
|
# 处理错误信息
|
|
|
|
|
tk.Label(self.result_frame,
|
|
|
|
|
text=results if isinstance(results, str) else results[0],
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
wraplength=500,
|
|
|
|
|
justify='left',
|
|
|
|
|
bg='white',
|
|
|
|
|
padx=10,
|
|
|
|
|
pady=10).pack(expand=True, fill='both')
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 创建表格框架
|
|
|
|
|
table_frame = tk.Frame(self.result_frame, bg='white')
|
|
|
|
|
table_frame.pack(expand=True, fill='both', padx=10, pady=10)
|
|
|
|
|
|
|
|
|
|
# 获取表头
|
|
|
|
|
headers = list(results[0].keys())
|
|
|
|
|
|
|
|
|
|
# 创建表头
|
|
|
|
|
for col, header in enumerate(headers):
|
|
|
|
|
tk.Label(table_frame,
|
|
|
|
|
text=header,
|
|
|
|
|
font=('微软雅黑', 10, 'bold'),
|
|
|
|
|
bg='#f0f0f0',
|
|
|
|
|
relief=tk.GROOVE,
|
|
|
|
|
padx=5,
|
|
|
|
|
pady=5).grid(row=0, column=col, sticky='ew')
|
|
|
|
|
|
|
|
|
|
# 填充表格内容
|
|
|
|
|
for row_idx, book in enumerate(results, start=1):
|
|
|
|
|
for col_idx, (key, value) in enumerate(book.items()):
|
|
|
|
|
tk.Label(table_frame,
|
|
|
|
|
text=value,
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
bg='white',
|
|
|
|
|
relief=tk.GROOVE,
|
|
|
|
|
padx=5,
|
|
|
|
|
pady=5).grid(row=row_idx, column=col_idx, sticky='ew')
|
|
|
|
|
|
|
|
|
|
# 设置列权重使表格均匀分布
|
|
|
|
|
for col in range(len(headers)):
|
|
|
|
|
table_frame.grid_columnconfigure(col, weight=1)
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app = GUI()
|
|
|
|
|
app.root.mainloop()
|