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.

73 lines
1.8 KiB

import tkinter as tk
from tkinter import messagebox
# 示例数据
items = [f"项目 {i + 1}" for i in range(50)]
# 定义搜索函数
def search_items():
search_term = entry_search.get().lower()
matches = [item for item in items if search_term in item.lower()]
if matches:
open_results_page(matches)
else:
messagebox.showinfo("搜索结果", "没有找到匹配的项目。")
# 定义显示搜索结果的函数
def open_results_page(matches):
results_window = tk.Toplevel(root)
results_window.title("搜索结果")
results_window.geometry("300x200")
label_results = tk.Label(results_window, text="搜索结果:", font=("Arial", 12))
label_results.pack(pady=10)
listbox_results = tk.Listbox(results_window, width=40, height=10)
listbox_results.pack()
for match in matches:
listbox_results.insert(tk.END, match)
# 创建主应用程序窗口
root = tk.Tk()
root.title("搜索框示例")
root.geometry("300x300")
# 创建搜索框标签和输入框
label_search = tk.Label(root, text="搜索:")
label_search.pack(pady=5)
entry_search = tk.Entry(root)
entry_search.pack(pady=5)
# 创建搜索按钮
btn_search = tk.Button(root, text="搜索", command=search_items)
btn_search.pack(pady=5)
# 创建一个框架来包含列表框和滚动条
frame = tk.Frame(root)
frame.pack(pady=20)
# 创建一个列表框
listbox = tk.Listbox(frame, width=40, height=10)
listbox.pack(side=tk.LEFT)
# 创建一个滚动条
scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 将滚动条与列表框关联
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# 向列表框中添加初始项目
for item in items:
listbox.insert(tk.END, item)
# 运行主循环
root.mainloop()