|
|
|
@ -1,42 +1,109 @@
|
|
|
|
|
# ui.py 文件
|
|
|
|
|
import tkinter as tk # 导入Tkinter库,用于GUI开发 [2](@ref)
|
|
|
|
|
import threading # 导入多线程模块,实现后台计算 [3](@ref)
|
|
|
|
|
import sys # 导入系统模块,用于路径管理 [2](@ref)
|
|
|
|
|
sys.path.append(r"./datas") # 将当前目录下的'one'文件夹加入模块搜索路径 [2](@ref)
|
|
|
|
|
from calculate import * # 动态导入b.py中的main函数,实现功能解耦 [2](@ref)
|
|
|
|
|
|
|
|
|
|
class GUI:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.root = tk.Tk() # 创建Tkinter主窗口对象 [2](@ref)
|
|
|
|
|
self.root.title('演示窗口') # 设置窗口标题 [2](@ref)
|
|
|
|
|
self.root.geometry("2500x560") # 设置窗口大小和位置 [2](@ref)
|
|
|
|
|
self.interface() # 调用界面布局方法 [2](@ref)
|
|
|
|
|
|
|
|
|
|
def interface(self):
|
|
|
|
|
""""界面编写位置"""
|
|
|
|
|
self.Button0 = tk.Button(self.root, text="确定查询", command=self.start, bg="#7bbfea") # 创建按钮控件 [2](@ref)
|
|
|
|
|
self.Button0.grid(row=0, column=1, pady=10,ipadx=25) # 使用grid布局管理器定位按钮 [2](@ref)
|
|
|
|
|
#command=self.start实现按钮点击事件绑定,避免直接操作主循环 。
|
|
|
|
|
#pady=10设置组件垂直间距,提升界面美观度
|
|
|
|
|
self.entry00 = tk.StringVar() # 创建字符串变量,用于数据绑定 [2](@ref)
|
|
|
|
|
self.entry00.set("") # 初始化变量值为空字符串
|
|
|
|
|
self.entry0 = tk.Entry(self.root, textvariable=self.entry00) # 创建输入框并绑定变量 [2](@ref)
|
|
|
|
|
self.entry0.grid(row=1, column=1, pady=15,sticky='n') # 布局输入框
|
|
|
|
|
'''
|
|
|
|
|
self.w1 = tk.Text(self.root, width=50, height=8) # 创建多行文本显示区域 [2](@ref)
|
|
|
|
|
self.w1.grid(row=2, column=0, columnspan=3, padx=60) # 布局文本区域
|
|
|
|
|
'''
|
|
|
|
|
self.result_label = tk.Label(self.root, text="查询结果将显示这里")
|
|
|
|
|
self.result_label.grid(row=3, column=0, columnspan=3)
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
isbn = self.entry00.get()
|
|
|
|
|
# 直接调用同步函数
|
|
|
|
|
result = query_isbn(isbn)
|
|
|
|
|
# 更新界面显示
|
|
|
|
|
self.result_label.config(text=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
a = GUI()
|
|
|
|
|
a.root.mainloop()
|
|
|
|
|
# ui.py 文件
|
|
|
|
|
import tkinter as tk
|
|
|
|
|
import threading
|
|
|
|
|
import sys
|
|
|
|
|
sys.path.append(r"./datas")
|
|
|
|
|
from calculate import *
|
|
|
|
|
|
|
|
|
|
class GUI:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.root = tk.Tk()
|
|
|
|
|
self.root.title('图书查询系统')
|
|
|
|
|
self.root.geometry("600x400")
|
|
|
|
|
self.root.configure(bg='#f0f0f0')
|
|
|
|
|
self.create_widgets()
|
|
|
|
|
self.setup_layout()
|
|
|
|
|
|
|
|
|
|
def create_widgets(self):
|
|
|
|
|
"""创建所有界面组件"""
|
|
|
|
|
# 主框架
|
|
|
|
|
self.main_frame = tk.Frame(self.root, padx=20, pady=20, bg='#f0f0f0')
|
|
|
|
|
|
|
|
|
|
# 标题
|
|
|
|
|
self.title_label = tk.Label(self.main_frame,
|
|
|
|
|
text="图书ISBN查询系统",
|
|
|
|
|
font=('微软雅黑', 16, 'bold'),
|
|
|
|
|
bg='#f0f0f0')
|
|
|
|
|
|
|
|
|
|
# 输入区域
|
|
|
|
|
self.input_frame = tk.Frame(self.main_frame, bg='#f0f0f0')
|
|
|
|
|
self.input_label = tk.Label(self.input_frame,
|
|
|
|
|
text="请输入ISBN:",
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
bg='#f0f0f0')
|
|
|
|
|
self.entry00 = tk.StringVar()
|
|
|
|
|
self.entry = tk.Entry(self.input_frame,
|
|
|
|
|
textvariable=self.entry00,
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
width=30,
|
|
|
|
|
relief=tk.GROOVE,
|
|
|
|
|
borderwidth=2)
|
|
|
|
|
|
|
|
|
|
# 查询按钮
|
|
|
|
|
self.query_button = tk.Button(self.main_frame,
|
|
|
|
|
text="查询",
|
|
|
|
|
command=self.start,
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
bg='#4CAF50',
|
|
|
|
|
fg='white',
|
|
|
|
|
activebackground='#45a049',
|
|
|
|
|
padx=15,
|
|
|
|
|
pady=5)
|
|
|
|
|
|
|
|
|
|
# 结果区域
|
|
|
|
|
self.result_frame = tk.Frame(self.main_frame,
|
|
|
|
|
bg='white',
|
|
|
|
|
relief=tk.GROOVE,
|
|
|
|
|
borderwidth=1)
|
|
|
|
|
self.result_label = tk.Label(self.result_frame,
|
|
|
|
|
text="查询结果将显示在这里...",
|
|
|
|
|
font=('微软雅黑', 10),
|
|
|
|
|
wraplength=500,
|
|
|
|
|
justify='left',
|
|
|
|
|
bg='white',
|
|
|
|
|
padx=10,
|
|
|
|
|
pady=10)
|
|
|
|
|
|
|
|
|
|
def setup_layout(self):
|
|
|
|
|
"""设置组件布局"""
|
|
|
|
|
self.main_frame.pack(expand=True, fill='both')
|
|
|
|
|
self.title_label.pack(pady=(0, 20))
|
|
|
|
|
|
|
|
|
|
# 输入区域布局
|
|
|
|
|
self.input_frame.pack(pady=10)
|
|
|
|
|
self.input_label.grid(row=0, column=0, padx=5)
|
|
|
|
|
self.entry.grid(row=0, column=1, padx=5)
|
|
|
|
|
|
|
|
|
|
# 按钮和结果区域
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
"""处理查询按钮点击事件"""
|
|
|
|
|
isbn = self.entry00.get().strip()
|
|
|
|
|
if not isbn:
|
|
|
|
|
self.result_label.config(text="请输入ISBN编号")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 禁用按钮防止重复点击
|
|
|
|
|
self.query_button.config(state=tk.DISABLED, text="查询中...")
|
|
|
|
|
self.root.update() # 强制更新界面
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
result = query_isbn(isbn)
|
|
|
|
|
if isinstance(result, dict):
|
|
|
|
|
formatted_result = "\n".join(f"{k}: {v}" for k, v in result.items())
|
|
|
|
|
self.result_label.config(text=formatted_result)
|
|
|
|
|
else:
|
|
|
|
|
self.result_label.config(text=result)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self.result_label.config(text=f"查询出错: {str(e)}")
|
|
|
|
|
finally:
|
|
|
|
|
# 恢复按钮状态
|
|
|
|
|
self.query_button.config(state=tk.NORMAL, text="查询")
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
app = GUI()
|
|
|
|
|
app.root.mainloop()
|