我正在尝试创建自己的消息框,该消息框具有一个关闭按钮,搜索标签,条目小部件和一个“开始”按钮,在其下方是一个滚动文本框,用于存储文本文件中的大量数据。搜索功能是让用户搜索该文件并弹出另一个消息框,其中包含找到的所有内容。我所有其他代码都工作正常,但是Entry窗口小部件出现问题。
我没有收到错误代码,但是它根本没有运行搜索。
但是我在下面没有正确完成吗?
这是自定义窗口中的代码:
class DisplayWindow(tk.Toplevel):
def __init__(self, title, content):
# code setting up how everything will look.
B1 = ttk.Button(main_frame, text="Close", command=self.destroy)
L1 = ttk.Label(main_frame, text="Search:")
self.e1 = ttk.Entry(main_frame)
B2 = ttk.Button(main_frame, text="Go", command=lambda: self.run_search)
B1.grid(row=1, column=0)
self.e1.grid(row=1, column=2)
L1.grid(row=1, column=1)
B2.grid(row=1, column=3)
def run_search(self):
s = log.search(self.title, self.e1.get())
return tk.messagebox.showinfo("search", s)
这是搜索的代码
def search(file, target):
if file == "current":
search_in = open("/home/pi/Desktop/NEA/current.txt", "r")
searchlines = search_in.readlines()
matching = [item for item in searchlines if target in item]
if any(target in item for item in searchlines):
return matching
else:
return "Search not found. Please try again."
search_in.close()
elif file == "records":
search_in = open("/home/pi/Desktop/NEA/records.txt", "r")
searchlines = search_in.readlines()
matching = [item for item in searchlines if target in item]
if any(target in item for item in searchlines):
return matching
else:
return "Search not found. Please try again."
search_in.close()
else:
return "Error finding file. Please try again."