You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
7.0 KiB

import tkinter as tk
import sql
from tkinter import messagebox
from tkinter import ttk
import webbrowser
import main
import GUI
db = sql.BookDatabase(host='localhost', user='root', password='123456', database='xiaosuo', table_name='user')
db.initialize_table()
db_data = sql.BookDatabase(host='localhost', user='root', password='123456', database='xiaosuo', table_name='books')
db_data.initialize_table()
book_collection=[]
search=""
Crawling=""
def on_login_button_click():
username = username_entry.get()
password = password_entry.get()
if username.strip() =="" or password.strip() == "":
messagebox.showinfo("登录失败", "用户名或密码不能为空")
return
user=db.login(username)
if user:
if password == user["password"]:
app.destroy()
# # 运行新窗口
show = tk.Tk()
show.title("数据显示页面")
show.geometry("600x400+200+100")
def serch_book(n):
print("我是一个测试")
if n == "搜索":
search = search_entry.get()
else:
search = ""
book_collection = db_data.get_book_list(search)
print(book_collection)
# 清空当前 Treeview
tree.delete(*tree.get_children())
book_id = 1
for book in book_collection:
tree.insert("", "end", text=book_id, values=(
book["name"], book["url"], book["star"], book["star_people"], book["author"],
book["translater"],
book["publisher"], book["pub_year"], book["price"], book["comment"]))
book_id = book_id + 1
tree.update()
def Crawling_book():
Crawling = Crawling_entry.get()
main.main(int(Crawling))
def analysis_book():
GUI.show()
def on_tree_double_click(event):
item = tree.selection()[0] # 获取双击的行
url = tree.item(item, "values")[1] # 假设 URL 地址在第二列,根据实际需要调整
webbrowser.open(url) # 打开 URL 地址
# 创建一个Frame来放置顶部的按钮和输入框
top_frame = tk.Frame(show)
top_frame.pack(side=tk.TOP)
search_button = tk.Button(top_frame, text="搜索", command=lambda: serch_book("搜索"), width=8, font=15)
search_button.pack(side=tk.LEFT)
search_entry = tk.Entry(top_frame, width=15)
search_entry.pack(side=tk.LEFT)
Crawling_button = tk.Button(top_frame, text="爬取", command=Crawling_book, width=8, font=15)
Crawling_button.pack(side=tk.LEFT)
Crawling_entry = tk.Entry(top_frame, width=15)
Crawling_entry.pack(side=tk.LEFT)
Refresh_button = tk.Button(top_frame, text="刷新", command=lambda: serch_book("刷新"), width=8, font=15)
Refresh_button.pack(side=tk.RIGHT)
analysis_button = tk.Button(top_frame, text="分析", command=analysis_book, width=8, font=15)
analysis_button.pack(side=tk.RIGHT)
# 显示书籍集合数据
book_collection = db_data.get_book_list()
tree = ttk.Treeview(show, columns=(
"name", "url", "star", "star_people", "author", "translater", "publisher", "pub_year", "price", "comment"))
tree.heading("#0", text="序号")
tree.heading("name", text="书名")
tree.heading("url", text="链接地址")
tree.heading("star", text="评分")
tree.heading("star_people", text="评分人数")
tree.heading("author", text="作者")
tree.heading("translater", text="译者")
tree.heading("publisher", text="出版社")
tree.heading("pub_year", text="出版年份")
tree.heading("price", text="价格")
tree.heading("comment", text="评论")
tree.column("#0", width=10) # 序号列
tree.column("name", width=30) # 书名列
tree.column("url", width=30) # 链接地址列
tree.column("author", width=20) # 作者列
tree.column("publisher", width=30) # 出版社列
tree.column("pub_year", width=30)
tree.column("price", width=5) # 价格列
tree.column("comment", width=30)
tree.column("star", width=30) # 评分列
tree.column("star_people", width=30) # 评分人数列
tree.column("translater", width=20)
book_id = 1
for book in book_collection:
tree.insert("", "end", text=book_id, values=(
book["name"], book["url"], book["star"], book["star_people"], book["author"], book["translater"],
book["publisher"], book["pub_year"], book["price"], book["comment"]))
book_id = book_id + 1
# 绑定双击事件
tree.bind("<Double-1>", on_tree_double_click)
# #布局管理器
# tree.grid(row=1, column=0, columnspan=2)
# 使用水平滚动条
tree.pack(fill="x")
# 运行应用程序
show.mainloop()
else:
messagebox.showinfo("登录失败", "密码错误")
else:
messagebox.showinfo("登录失败", "{}用户不存在".format(username))
# 创建 Tkinter 应用程序
app = tk.Tk()
app.title("登录界面")
app.geometry("500x450+400+100")
# 创建用户名和密码输入框
username_label = tk.Label(app, text="用户名",font=16)
username_label.pack(pady=(120,0))
username_entry = tk.Entry(app,width=30)
username_entry.pack()
password_label = tk.Label(app, text="密码",font=16)
password_label.pack()
password_entry = tk.Entry(app, show="*",width=30)
password_entry.pack()
login_button = tk.Button(app, text="登录", command=on_login_button_click,width=15,font=15)
login_button.pack()
# 运行应用程序
app.mainloop()
# 数据显示页面
def show_data():
search_window = tk.Toplevel()
search_window.title("搜索页面")
# 创建搜索框
search_label = tk.Label(search_window, text="搜索:")
search_label.pack(pady=(20, 0))
search_entry = tk.Entry(search_window, width=30)
search_entry.pack()
# 显示书籍集合数据
book_collection = db.get_book_list()
show_book_collection(search_window, book_collection)
# 运行新窗口
search_window.mainloop()
def show_book_collection(window, book_collection):
tree = ttk.Treeview(window)
tree["columns"] = ("name", "author", "publisher", "price")
tree.heading("#0", text="ID")
tree.heading("name", text="书名")
tree.heading("author", text="作者")
tree.heading("publisher", text="出版社")
tree.heading("price", text="价格")
for book in book_collection:
tree.insert("", "end", text=book.id, values=(book.name, book.author, book.publisher, book.price))
tree.pack()
window.mainloop()