import tkinter as tk import sql from tkinter import messagebox from tkinter import ttk 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="" def on_login_button_click(): username = username_entry.get() password = password_entry.get() if db.login(username, password): print("登录成功!") app.destroy() # 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) # # 运行新窗口 show = tk.Tk() show.title("数据显示页面") show.geometry("600x400+200+100") # 创建搜索框 # search_label = tk.Label(show, text="搜索:", pady=10) # search_label.pack() def serch_book(): book_collection = db_data.get_book_list(search) print("我搜索了:") # 清空当前 Treeview tree.delete(*tree.get_children()) book_id = 1 for book in book_collection: print(type(book)) 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 search_button = tk.Button(show, text="搜索", command=serch_book, width=30, font=15) search_button.pack() search_entry = tk.Entry(show, width=30) search=search_entry.get() search_entry.pack() # 显示书籍集合数据 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: print(type(book)) 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.grid(row=1, column=0, columnspan=2) # 使用水平滚动条 tree.pack(fill="x") # 运行应用程序 show.mainloop() else: messagebox.showinfo("登录失败", "用户名或密码错误") # 创建 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()